How to work with jQuery AJAX and PHP array return

I have a jQuery ajax request, for example:

$.ajax({ type: 'POST', url: 'processor.php', data: 'data1=testdata1&data2=testdata2&data3=testdata3', cache: false, success: function(result) { if(result){ alert(result); }else{ alert("error"); } } }); 

The processor.php handler is set to return an array, for example:

 $array = array("a","b","c","d"); echo $array; 

I want to do a client side action based on this. Say if array [0] is 'b', I want to warn hi. Again, if array [2] is "x", I want to warn "hello", etc. How can I filter array elements to capture their data?

+7
source share
5 answers

You will need to return an array encoded in json form as shown below.

 $array = array("a","b","c","d"); echo json_encode($array); 

then you can access it in javascript by converting it back to an array / object, e.g.

 var result = eval(retuned_value); 

You can also move through all elements of the array using the for loop

 for (var index in result){ // you can show both index and value to know how the array is indexed in javascript (but it should be the same way it was in the PHP ) alert("index:" + index + "\n value" + result[index]); } 

in your code it should look something like this:

PHP code:

 $array = array("a","b","c","d"); echo json_encode( $array ); 

jQuery script

 $.ajax({ type: 'POST', url: 'processor.php', data: 'data1=testdata1&data2=testdata2&data3=testdata3', cache: false, success: function(result) { if(result){ resultObj = eval (result); alert( resultObj ); }else{ alert("error"); } } }); 
+22
source

JSON return from php http://php.net/manual/en/function.json-encode.php

and in javascript create an object from json string, you can do it with getJSON instead of ajax http://api.jquery.com/jQuery.getJSON/

Make sure your php sets the correct response header:

  header ("content-type: application/json; charset=utf-8"); 
+2
source

I find the best way to return an array from php to Ajax (jscript):

on the php side: echo json_encode($myArray); on the javascript side (e.g. myAjax.responseText ) replyVal = JSON.parse(myAjax.responseText);

To send arrays to php from javascript, use JSON.stringify() mapping to send and php json_decode() to get

+1
source

In your PHP code, encode the array as a JSON object

 echo json_encode($array); 

Then you need to convert the JSON object to a Javascript / jQuery-compatible object. After that you can convert back to an array

 $.ajax({ success: function(result) { jq_json_obj = $.parseJSON(result); //Convert the JSON object to jQuery-compatible if(typeof jq_json_obj == 'object'){ //Test if variable is a [JSON] object jq_obj = eval (jq_json_obj); //Convert back to an array jq_array = []; for(elem in jq_obj){ jq_array.push(jq_obj[elem]); } console.log(jq_array); }else{ console.log("Error occurred!"); } } }); 
0
source
 $.ajax({ type: 'POST', url: 'processor.php',//please return in json dataType : 'json',//to get data in json data: 'data1=testdata1&data2=testdata2&data3=testdata3', cache: false, success: function(result) { if(result.array.length > 0){ for(var i=0;i<result.array.length;i++){ if(result.array.[i]== 'a'){ //do somthing.. //here, you can use switch case too... } } } } }); 
-one
source

All Articles