1

I have a php function which I am using to calculate statistics of information in a database. I want to output the results in a string and pass it to JQuery to be able to make graphical representations of the data.

Is it possible to pass a string from PHP into a JQuery method in this way?

Thanks in advance

asked Jul 28, 2013 at 12:08
1

3 Answers 3

1

Jquery is javascript was executed by browser.

Set the header as javascript on your php and load with script tag.

<script type="text/javascript" src="/yourphpfile.php"></script

And then in file yourphpfile.php

<?php
 header('Content-type:application/javascript');
 $stringFromDb = functionGetContentFromDB();
?>
var data = "<?= $stringFromDb ?>";

List item

answered Jul 28, 2013 at 12:18

Comments

0
 <?php
 $var = "somestring";
 echo '<script>var str = "'.$var.'";</script>';
 ?>

you can do that this way

answered Jul 28, 2013 at 12:11

Comments

0

There are two ways, you can output this into the javascript onload, echoing to the inline javascript.

<script>
 var string = "<?php echo $string; ?>";
</script>

Alternatively you can fetch the data from ajax using json_encode in php to get the data in the correct format.

If you then use getJson (http://api.jquery.com/jQuery.getJSON/) you will have the data in the correct format.

answered Jul 28, 2013 at 12:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.