[Pro. PHP Dev.] passing mysql query result to java script varible

45 views
Skip to first unread message

srini vasan

unread,
May 18, 2010, 4:14:49 AM5/18/10
to Professi...@googlegroups.com
hi
i need to pass the mysql query result to java script variable. how i achieve that.
 var address - my java script variable
select address from markers - is my query

how i pass result of this query to this variable

                    thankx

                           

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
 
For information or project assistance please visit :
http://www.360psg.com
 
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP

Mityok

unread,
May 18, 2010, 5:16:48 AM5/18/10
to Professional PHP Developers
Hi

In a case you don't want/have to use AJAX, you need to write
Javascript code using PHP.
For example, in your HTML code you can place a code like that:
<script type="text/javascript" src="yourphpscript.php"></script>

In "yourphpscript.php" you can write something like that :
<?php
$results = array();
//Obtain results from DB and prepare them to be passed to Javascript
....
?>

var address = <?php echo $results; ?>

Another way - AJAX requests.

srini vasan

unread,
May 18, 2010, 5:32:41 AM5/18/10
to professi...@googlegroups.com
thanks,
how it will be achieved  if  i using AJAX requests

Mityok

unread,
May 18, 2010, 6:29:47 AM5/18/10
to Professional PHP Developers
What Javascript library do you use (jQuery, Prototype,....) ?

srini vasan

unread,
May 18, 2010, 6:30:44 AM5/18/10
to professi...@googlegroups.com
jquery

Eric Wilson

unread,
May 18, 2010, 9:18:34 AM5/18/10
to Professional PHP Developers
from your AJAX RPC, encode your php array as a JSON encoded string
(http://www.php.net/manual/en/function.json-encode.php):

. . .
$ret = json_encode($array_containing_your_data);
header("Content-Type: application/json");
echo $ret;
. . .

not sure about jQuery but in the onComplete section of your AJAX call
in prototype you would then be able to do this:

onComplete: function(transport)
{
$('form_field1').innerHTML = transport.responseJSON.some_field_name;
$('form_field2').value = transport.responseJSON.another_field_name;
Reply all
Reply to author
Forward
0 new messages