将JSON AJAX回复传递给PHP

I have a script which uses AJAX to connect to a PHP script which queries a database and returns some values. The code of which is below:

<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajaxphp.php?ID="+str,true);
xmlhttp.send();
}
</script>

<select id="users" name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<!-- PHP populates this dropdown box -->
</select>
<div id="txtHint"><b>Selected user info will be listed here.</b></div>

Right now the txtHint div will return anything the ajaxphp.php script prints. This isn't very flexible, however. What I want to do is create an array in ajaxphp.php and use json_encode() to pass the results back.

The problem I'm having is I don't know how to get the original script to grab the results so I can do useful things with them. Right now I can make it return a JSON array which will appear in the txtHint div but I've no idea how to get PHP to actually read that information so I can do something with it.

Use $_GET method to See what ever the user send you in php see here:

http://php.net/manual/en/reserved.variables.request.php

Maybe the json_decode() php method is the solution of what you want.

http://www.php.net/manual/en/function.json-decode.php

This method takes a JSON encoded string (from the json_encode method for example) and converts it into a PHP variable... so you can use this variable like an object and simply access to its attributes.

Maybe this other post will help you : How to decode a JSON String with several objects in PHP?

Hope this helps ! Bye !

Try using jQuery Ajax...

    $.ajax({
        url : 'ajaxphp.php?ID='+str,                          
        type: 'get',                   
        dataType:'json',                   
    success : function(data) {  
        console.log(data);
    }
   });

Data in success function parameter is your encoded result that you return from php.

echo json_encode($result); 

Then you can access it with something like this from javascript.

data.result1
data.result2
data.result3....