1

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.

Roy
  • 705
  • 2
  • 11
  • 32
  • If you can get your PHP to return a JSON array, then it's your javascript function that needs changing, not php. Instead of `document.getElementById("txtHint").innerHTML=xmlhttp.responseText;`, you'll have your code to parse the array and do something useful with it. – Aleks G Apr 22 '13 at 08:28
  • On another topic, I suggest you have a look at an existing library, e.g. jQuery, which takes care of a lot of things for you, including handling browser differences and parsing the response back into a javascript array. – Aleks G Apr 22 '13 at 08:28

3 Answers3

1

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

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

AboQutiesh
  • 1,696
  • 2
  • 9
  • 14
1

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 !

Community
  • 1
  • 1
Antoine
  • 548
  • 4
  • 13
  • I understand json_decode() exists, the problem is I don't know how to pass the result of the AJAX script to json_decode() – Roy Apr 22 '13 at 08:36
1

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....
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Djomla
  • 620
  • 2
  • 7
  • 18
  • Thanks for your response, I've got everything working up until the last point. I'm not very familiar with Javascript so I'm not sure how to then access that data as you say. I want to use it to populate various form fields and may require some processing in the future. Would there be any way to get the json encoded result back to PHP? – Roy Apr 22 '13 at 08:52
  • 1
    Try reading this http://stackoverflow.com/questions/4424518/accessing-json-data-from-jquery It's really easy to handle json object in javascript. Just open console in you browser, and see result from console.log(data); command – Djomla Apr 22 '13 at 09:13