0

the title sounds weird, but here's my question. I am making this loop in Javascript that needs to call PHP variables.

    var data = new Array("<?php echo count($result) ?>");
    for (var i=0; i < "<?php echo count($result) ?>"; i++) {
        data[i] = "<?php echo $result[i] ?>";
    }

The third line is the problem. I tried

            data[i] = "<?php echo $result [" + i + " ] ?>";

but it didn't work.

Any clever tip to solve this??

2
  • Are you really trying to call PHP code from JavaScript? Or these snippets of code come from a server side html+javascript generated by PHP? Commented May 11, 2012 at 2:39
  • @Nicolás this code is from a PHP file, and I need the javascript for d3.js. Commented May 11, 2012 at 9:48

1 Answer 1

3
var data = <?= json_encode($result); ?>;
2
  • 1
    +1; This is really the most elegant (and secure!) way to do it. Commented May 11, 2012 at 2:40
  • oh it did not show up when I used jQuery.html(), but now I checked it works! Thanks a lot!! Commented May 11, 2012 at 12:02

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.