how to get value from ajax response in php?

how to get value from ajax response in php – return value in php after an ajax call by javascript. Look at the response returned from your AJAX request

how to get value from ajax response in php?

JQuery Ajax Call to PHP Script with JSON return using PHP server side code. Steps to make Ajax POST request with JQuery and PHP

fetch.php

<?php
if(isset($_POST["id"]))
{
 $link = mysqli_connect("localhost", "root", "", "pakainfo_v1");
 $query = "SELECT * FROM member WHERE id = '".$_POST["id"]."'";
 $result = mysqli_query($link, $query);
 while($results = mysqli_fetch_array($result))
 {
  $data["name"] = $results["name"];
  $data["address"] = $results["address"];
  $data["gender"] = $results["gender"];
  $data["info"] = $results["info"];
  $data["age"] = $results["age"];
 }

 echo json_encode($data);
}
?>

simple return data jquery

$(document).ready(function(){
    $.ajax({
        url: 'sendData.php',
        type: 'get',
        dataType: 'JSON',
        success: function(response){
            var len = response.length;
            for(var i=0; i<len; i++){
                var id = response[i].id;
                var membername = response[i].membername;
                var name = response[i].name;
                var email = response[i].email;

                var tr_str = "<tr>" +
                    "<td align='center'>" + (i+1) + "</td>" +
                    "<td align='center'>" + membername + "</td>" +
                    "<td align='center'>" + name + "</td>" +
                    "<td align='center'>" + email + "</td>" +
                    "</tr>";

                $("#memberTable tbody").append(tr_str);
            }

        }
    });
});

Example : simple return data jquery

<?php

include "config.php";

$return_arr = array();

$query = "SELECT * FROM members ORDER BY NAME";

$result = mysqli_query($con,$query);

while($rData = mysqli_fetch_array($result)){
    $id = $rData['id'];
    $membername = $rData['membername'];
    $name = $rData['name'];
    $email = $rData['email'];

    $return_arr[] = array("id" => $id,
                    "membername" => $membername,
                    "name" => $name,
                    "email" => $email);
}

// Encoding array in JSON format
echo json_encode($return_arr);

How to get values of the AJAX Response?

<script>
$(document).ready(function(){
 $('#search').click(function(){
  var id= $('#member_list').val();
  if(id != '')
  {
   $.ajax({
    url:"fetch.php",
    method:"POST",
    data:{id:id},
    dataType:"JSON",
    success:function(data)
    {
     $('#member_details').css("display", "block");
     $('#member_name').text(data.name);
     $('#member_address').text(data.address);
     $('#member_gender').text(data.gender);
     $('#member_info').text(data.info);
     $('#member_age').text(data.age);
    }
   })
  }
  else
  {
   alert("Please Select member");
   $('#member_details').css("display", "none");
  }
 });
});
</script>

don’t Miss : How To Return Value From Ajax Success Function In Javascript?

I hope you get an idea about how to get value from ajax response in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.