Skip to content Skip to sidebar Skip to footer

How To Get Multiple Responses To A Single Ajax Request In Php

I am making a web app, where I want to provide a search function. I am sending the searched name with an ajax request, and i want to pull the records of that particular person. But

Solution 1:

Use JSON as the datatype to communicate between PHP(Backend) and Javascript(Frontend). Example:

PHP

<?$person = array("name"=>"Jon Skeet","Reputation"=>"Infinitely Increasing");
header("Content-Type: application/json");
echo json_encode($person);
?>

Javascript/jQuery

$.ajax({
  url: "your_script.php",
  dataType: "JSON"

}).success(function(person) {
  alert(person.name) //alerts Jon Skeet
});

Solution 2:

Add everything you want to an array, then call json_encode on it.

$data = array();

$data[] = $person1;

$data[] = $person2;

echo json_encode($data);

Post a Comment for "How To Get Multiple Responses To A Single Ajax Request In Php"