How to pass an array of strings from PHP to Javascript using $ .ajax ()?

I have a PHP script that extracts names (strings) from a database. I would like to pass this array to Javascript using $ .ajax (). I cannot figure out how I should encode an array in PHP and then decode it in Javascript. Can someone give some sample code for this? Many thanks!

+5
javascript jquery ajax php
source share
5 answers
<?php // test.php $myArray = array(1, 2, 3); echo json_encode($myArray); ?> 

HTML file:

 $(function() { $.getJSON('http://localhost/test.php', function(data) { $(data).each(function(key, value) { // Will alert 1, 2 and 3 alert(value); }); }); }); 
+3
source share

u can use json_encode

 <?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); ?> 

full example that you can read at:

http://www.prodevtips.com/2008/08/15/jquery-json-with-php-json_encode-and-json_decode/

or

http://www.prodevtips.com/2009/12/09/multiple-select-lists-with-jquery-and-json/

+2
source share
 <?php echo json_encode(array('key' => 'value', 'cool' => 'ice')); ?> 

json is a javascript object. Therefore, there is no need to "decode" it. However, it looks like you are using jquery. There is a great feature to extract json data:

 jQuery.getJSON(url, senddata, function(returndata){alert(returndata.cool);}) 

or

 jQuery.getJSON(url, senddata, function(returndata){mybigfunction(returndata);}) mybigfunction(data) { myvar = data.cool; alert(myvar); } 

http://api.jquery.com/jQuery.getJSON/

or you can also do this with $ .ajax, as you mentioned:

 jQuery.ajax({ url: url, dataType: 'json', data: senddata, success: function(data){mybigfunction(data)} }); mybigfunction(data) { myvar = data.cool; alert(myvar); } 

http://api.jquery.com/jQuery.ajax/

A callback is a function that is called and passes the json data returned from the url.

You will be the "ice" of the child ... ermm ... sorry for the corn.

The getJSON method is quite short and convenient. See the links for more details.

+1
source share

To do this, you just need to output the script to a PHP page containing your data, and then you can access it from any other Javascript on the page, including jQuery and .ajax ().

Again, if you just want to pass it through an AJAX call, just use json_encode ():

 <?php echo json_encode( array( 'groupidlist'=>$groupids, 'groupnamelist'=>$groupnames, 'serverurl'=>$serverurl, 'uid'=>$curuser->getID() ) ); ?> 

And then handle it with callback functions from .ajax() or, perhaps better, .getJSON() , which is built for this purpose only.

I promise that it's not just spamming my blog here, but I wrote a message about passing variables between Javascript and PHP , because I did it often enough to come up with a simple / reliable / clean and reusable way to do this. If you regularly transfer data from PHP to Javascript and do not need AJAX, I will insert the basic information here:

At the top of each external js file, I add comments regarding what PHP variables are needed, so I can keep track of what I need when I turn it on (this is optional, of course, but nice):

 /* This script depends on the following variables in phpvars: groupidlist groupnamelist serverurl uid */ 

Then in the PHP file, I pass the necessary variables with a single line of Javascript, assigning a JSON array with all the necessary values. Examples in PHP directly from my code:

 <script type="text/javascript"> var phpvars = <?php echo json_encode( array( 'groupidlist'=>$groupids, 'groupnamelist'=>$groupnames, 'serverurl'=>$serverurl, 'uid'=>$curuser->getID() ) ); ?>; </script> 

Once this is set up, I can simply access any PHP variables that I need in Javascript through the phpvars array. For example, if I need to set the image source using my serverurl, I could do the following:

 imgElement.src = phpvars.serverurl + '/images/example.png'; 

Since it uses JSON, there is no need to worry about not wrapping anything in your Javascript while trying to insert PHP variables. Encoding / decoding of variables is processed at both ends by the built-in JSON functions, so it is very difficult to break them and send the variables brainlessly - you pass them in the same way as any other PHP array. In my game, which led to this, I had problems with both of them, and this solution will take care of them beautifully.

0
source share

This is the php file code

 <?php $array = array(1, 2, 3); echo json_encode($array); ?> 

Then you can $.ajax() $array into $.ajax() like this

 success: function (data) { var x = JSON.parse(data); console.log(x); } 
0
source share

All Articles