0

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>';
?>
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 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 ` – 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 "";`. 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
  • Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/users1/std10093/public_html/scheduler.php on line 356 With the only uncommented echo in my code and type=.." – gsamaras Dec 27 '13 at 23:11
  • @Kimmax I got (with your code) Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/users1/std10093/public_html/scheduler.php on line 356 I do not know how to use json – gsamaras Dec 27 '13 at 23:12
  • @user2411320 what is the value of $filetext? – iMx Dec 27 '13 at 23:14
  • I'm sorry `echo "";` should do the trick. Well, have a look at Json, its easier for you.. – Kimmax Dec 27 '13 at 23:14
  • @ShinTakezou no, it's a string of strings! So what should I do? :/ – gsamaras Dec 27 '13 at 23:14
  • @Kimmax Uncaught SyntaxError: Unexpected token ILLEGAL with the updated code. Maybe you are right, but I want to have a workable solution with this one first. :) iMx, I am reading from a file.txt of format name1 name2 .. – gsamaras Dec 27 '13 at 23:16

4 Answers4

1

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>
Royal Bg
  • 6,988
  • 1
  • 18
  • 24
1

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.

iMx
  • 846
  • 1
  • 9
  • 23
  • 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: http://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
  • @user2411320 now you have the T_STRING error on the page. Check this fiddle: http://jsfiddle.net/as2fk/ I get the same error in this case. – iMx Dec 27 '13 at 23:37
  • THAT'S IT! THAT'S THE WRITE ANSWER! ;) (the line breaks!) – gsamaras Dec 27 '13 at 23:37
0

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
Community
  • 1
  • 1
Mehmet
  • 1,824
  • 3
  • 19
  • 28
0

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>';
gsamaras
  • 71,951
  • 46
  • 188
  • 305