The case is that I have a .php file and inside it, I have a function inside script tags. After it, I have php code, that reads from a file. I want to sent the file data to the js function. I had done this before, but now it will produce parsing errors.

SOLUTION

THE file format must not have line breaks!!!!
echo '<script>updateMatch1("'.$filetext.'") </script>';

Here is the code

<script>
    function updateMatch1(names) {
    alert(names);
};
</script>
<?php
    /* Read file */
    ...
    $filetext = fread( $file, $filesize ); //checked the output, it's ok

    // numerous ways I tried. Some produce an error and the page isn't loaded at all,
        // while others produce an error in the console
    echo "<script>updateMatch1('" . $filetext . "');</script>";
    //echo '<script> updateMatch1($filetext);</script>';
    //echo '<script>updateMatch1();</script>';
    //echo "<script>updateMatch1($filetext" . ")</script>";
    //echo "<script>updateMatch1($filetext)</script>";
        //echo '<script>updateMatch1(' . $filetext  . ');</script>';
?>
share|improve this question

closed as off-topic by Quentin, cryptic ツ, Dagg Nabbit, hakre, Michael Hampton Dec 28 '13 at 2:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." – Quentin, cryptic ツ, hakre, Michael Hampton
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
"produce an error — What error? If you have JavaScript errors, then show us the JS you actually send to the browser, not the PHP that generates it. – Quentin Dec 27 '13 at 23:08
1  
what if $filetext is something like I'm not a cage? – ShinTakezou Dec 27 '13 at 23:09
    
did you try json_encode and json_decode? also did you try using <script type="text/javascript"> ? – Kyborek Dec 27 '13 at 23:09
    
Please provide the Error message and the value of the $filetext variable – iMx Dec 27 '13 at 23:10
    
You can use echo "<script type="text/javascript">updateMatch1('$filetext');</script>";‌​. PHP identifys the variable. Look at the source code and see what the recived file looks like. As said above, try Json. – Kimmax Dec 27 '13 at 23:10
up vote 1 down vote accepted

Check your file.txt. Maybe it contains some illegal character or has some incompatible file encoding that produce the illegal character. If you print the value of $filetext with php, there is no visible error, but it can produce some in JS. E.g. it can be the zero-width space. See if you have spaces or other characters on the end of the file.

share|improve this answer
    
The file is of format name \n name \n (I use \n because the comment will eat my newlines). It seems ok. What should I check more? – gsamaras Dec 27 '13 at 23:25
    
@user2411320 can you provide the HTML code of the generated page on this place? I need the code on the position of the Javascript function call with the parameter. – iMx Dec 27 '13 at 23:33
    
@user2411320 try to remove the line breaks from the text file and test it again. And check your HTML. It will not work if PHP makes line breaks in the JS param string. – iMx Dec 27 '13 at 23:34
    
You need the whole file? If so, I can post it. If you mean the link: cgi.di.uoa.gr/~std10093/scheduler.php – gsamaras Dec 27 '13 at 23:34
    
The line breaks must be the key! :D – gsamaras Dec 27 '13 at 23:35

If you did not hide anything from your code, then this dots are producing parse error

/* Read file */
    ...

You should get rid of ...

Also, have in mind that:

<?php $filetext = "alalala"; ?>
<script>
    updateMatch1('<?=$filetext;?>');
</script>

Will produce the correct alert 'alalala', but:

<?php $filetext = "alal'ala"; ?>
<script>
    updateMatch1('<?=$filetext;?>');
</script>

will produce:

SyntaxError: missing ) after argument list


updateMatch1('alal'ala');

Escape your file output before pass to js.

You can try a simple escape:

<?php $filetext = "alal'ala"; ?>
<script>
    updateMatch1('<?= htmlspecialchars($filetext, ENT_QUOTES);?>');
</script>
share|improve this answer
    
That's code for manipulating the file. :) – gsamaras Dec 27 '13 at 23:19
    
See my edit :) I guess you are sending data to the JS that can break the syntax because of lack of escaption. – Royal Bg Dec 27 '13 at 23:22
    
That maybe the case, but I really do not know what to do! When I print the data of the file with print_r(), it will output: name1 name2 name3 – gsamaras Dec 27 '13 at 23:23
    
I guess you can use htmlspecialchars() or htmlenetities() – Royal Bg Dec 27 '13 at 23:29
    
I am trying to make them run, but I am getting Parse error: syntax error, unexpected T_STRING Can you please provide me some tip? – gsamaras Dec 27 '13 at 23:33

There are lots of dupicate questions on stackoverflow dont ask everything just search bro!!!

For samples:

  1. how to pass php parameter to javascript
  2. How to pass JavaScript variables to PHP?
  3. Passing parameters to Javascript using PHP
share|improve this answer
    
Please read carefully the post of the OP's next time. :) – gsamaras Dec 27 '13 at 23:19

Answer was given by @iMx!!

The reason was that the .txt file contained line breaks. These would break the syntax. The solution was to separate the names by whitespaces, instead of newlines, and everything was ok!

echo '<script>updateMatch1("'.$filetext.'") </script>';
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.