returns I have a php script which is called by ajax and i do some operation on mysql in this file and now i want to return the result stored in php array to ajax call. So how it can be possible.
6 Answers
Try this:
Use json for that. Read this for more details.
JSON is used especially for this purpose.
--
Thanks
4 Comments
json (even though its a good idea to use json in a case like this) it don't really answer the question.json_encode which, like i said, encodes data to json, it don't tell the OP how to access the data in the ajax callbacks or anything, just shows him how to formate the data into another type of object (which he might as well have done already for all we know). :PIf you want to pass PHP Array to an AJAX call convert it as JSON and pass it.
The below is an example
<?php
$arr = array('a' => 10, 'b' => 23, 'c' => 32, 'd' => 42, 'e' => 52);
echo json_encode($arr);
?>
2 Comments
success: function(arr) { var alphaNum = JSON.parse(arr); var a = alphaNum.a; //10 var b = alphaNum.b; //23 }Anything you print in the php script will be 'returned' to the ajax call and caught in the success callback.
$.ajax({
type: "POST",
url: url,
data: data,
success: function(response){
alert(response); // Will print whatever the php script 'echos' out.
// or with the php code below as the script you call, you can use:
if(response.result) {
alert(response.data); // For the data in 'data' to be printed.
// Where response.data is your array:
for(var i=0;i<response.data.length;i++){
console.log(response.data[i]);
}
} else {
console.log(response.error);
}
},
});
Edit: As other states in the other answers, json is perfect to return from a php script to an ajax callback, cause JavaScript and PHP both handles this very well.
<?php
// I like to return an associative array,
// which in the javascript part of the code can be used as a object.
// I always include a 'result' boolean value which will let me know on the js
// side if the request was successfully done or not, and if false,
// i usually include a 'error' property with a error message.
$myreturndata = array("result" => true, "data" => $yourArray, "error" => null);
header('Content-Type: application/json');
die(json_encode($myreturndata));
Comments
A useful example on top of the other answers. Splitting each array value into javascript variables.
Improved with useful advice :)
PHP
echo json_encode($array);
jQuery.ajax in your success function
// pass each value to variables (this example array looks like this: [100,200,300])
var variableOne = response[0]; // This variable will = 100
var variableTwo = response[1]; // This variable will = 200
var variableThree = response[2]; // This variable will = 300
// Now you can easily use each individual value.
alert(variableOne);
4 Comments
[100,200,300] and there is no reason to parse/stringify it, if its JSON that is returned, javascript can use it like a standard javascript object/array: alert(response[0]) <- will be 100. But its always best to validate the response of course.alert(response[0]) gives me: [alert(response[0]) works fine with it. I will update my answer, it was off the top of my head anyway.