1

I can't escape my code JS in my echo :

echo "<script type='text/javascript'>
    // Initialiser l'objet.
    var tab_nb_match_par_user = ".json_encode($tab_nb_match_par_user).";
    var taille = 70;    
    console.log(tab_nb_match_par_user);
    for (var k in tab_nb_match_par_user){
        $('#img').append('<img src="stats_matching/' + k + '.gif" alt="' + k + '" title="' + k + '" width="' + taille + '" />');
        taille = taille-6;
    }
</scrip>";

And I have this follow error: Uncaught SyntaxError: missing ) after argument list

3
  • 2
    You are missing a "t" in your </script> end tag. Commented Jun 24, 2015 at 14:23
  • And you seem to have some wrong quotes there. If you want to use double quote ( " ) in your javascript you have to escape them like \" Commented Jun 24, 2015 at 14:25
  • thanks @Gunni , I found my error Commented Jun 24, 2015 at 14:30

4 Answers 4

3

Use HEREDOC instead of echo.

echo <<<ENDOFSCRIPT
<script type='text/javascript'>

                    // Initialiser l'objet.
                    var tab_nb_match_par_user = ".json_encode($tab_nb_match_par_user).";
                    var taille = 70;    
                    console.log(tab_nb_match_par_user);
                    for (var k in tab_nb_match_par_user){
                        $('#img').append('<img src="stats_matching/' + k + '.gif" alt="' + k + '" title="' + k + '" width="' + taille + '" />');
                        taille = taille-6;
                    }
            </script>

ENDOFSCRIPT;

more info at https://wiki.php.net/rfc/heredoc-with-double-quotes

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

Comments

2

Assuming that you only want to actually have php fill in the json part, why not just echo in the bit you need, rather than the whole structure. This is much better practice in templating.

<script type='text/javascript'>
    // Initialiser l'objet.
    var tab_nb_match_par_user = "<?php echo json_encode($tab_nb_match_par_user) ?>";
    var taille = 70;    
    console.log(tab_nb_match_par_user);
    for (var k in tab_nb_match_par_user){
        $('#img').append('<img src="stats_matching/' + k + '.gif" alt="' + k + '" title="' + k + '" width="' + taille + '" />');
        taille = taille-6;
    }
 </script>

you were also missing a 't' in your end script tag.

Comments

2
echo <<<END
    <script type="text/javascript">
        // Initialiser l'objet.
        var tab_nb_match_par_user = ".json_encode($tab_nb_match_par_user).";
        var taille = 70;    
        console.log(tab_nb_match_par_user);
        for (var k in tab_nb_match_par_user){
            $('#img').append('<img src="stats_matching/' + k + '.gif" alt="' + k + '" title="' + k + '" width="' + taille + '" />');
            taille = taille-6;
        }
    </script>
END;

Comments

1

You need to escape the code using a back slash:

echo "<script type='text/javascript'>

                        // Initialiser l'objet.
                        var tab_nb_match_par_user = ".json_encode($tab_nb_match_par_user).";
                        var taille = 70;    
                        console.log(tab_nb_match_par_user);
                        for (var k in tab_nb_match_par_user){
                            $('#img').append('<img src=\"stats_matching/' + k + '.gif\" alt=\"' + k + '\" title=\"' + k + '\" width=\"' + taille + '\" />');
                            taille = taille-6;
                        }
                </scrip>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.