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>';
?>
11
  • 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. Commented Dec 27, 2013 at 23:08
  • 1
    what if $filetext is something like I'm not a cage? Commented Dec 27, 2013 at 23:09
  • did you try json_encode and json_decode? also did you try using <script type="text/javascript"> ? Commented Dec 27, 2013 at 23:09
  • Please provide the Error message and the value of the $filetext variable Commented Dec 27, 2013 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. Commented Dec 27, 2013 at 23:10

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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?
@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.
@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.
You need the whole file? If so, I can post it. If you mean the link: cgi.di.uoa.gr/~std10093/scheduler.php
The line breaks must be the key! :D
|
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>

6 Comments

That's code for manipulating the file. :)
See my edit :) I guess you are sending data to the JS that can break the syntax because of lack of escaption.
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
I guess you can use htmlspecialchars() or htmlenetities()
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?
|
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

1 Comment

Please read carefully the post of the OP's next time. :)
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>';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.