Retrieve multiple values from php file using AJAX?

How would i retreive multiple (GET) values from a php file using AJAX…

Say i had the following function that ask’s for a userID variable - how could i ask for more than one variable instead of just userID? so i can change the html with:

ajaxDisplay1.innerHTML = ajaxRequest.responseText;
ajaxDisplay2.innerHTML = ajaxRequest.responseText;


  ajaxRequest.onreadystatechange = function() {
    if(ajaxRequest.readyState == 4) {
      var ajaxDisplay = document.getElementById("tagButton");
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
  }
  var userID = 1581813101;
  var queryString = "?userID="+userID;
  ajaxRequest.open("GET", "test.php" + queryString, true);
  ajaxRequest.send(null);
}

And here’s the test.php file code


<?php
include('db.php');
$userID = $_GET['userID'];
$query = "SELECT firstName, lastName FROM user_details WHERE profileID = $userID";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$userID = $row[firstName];

echo $userID;
?>

Possibly get the PHP file to echo the result as a comma-separated list (or use some other character, like ¬), then split() the response text into an array and use each array element as the innerHTML for each display area?

Thanks! worked a treat

Hi, You should look in to JSON. It allows you to return as much info from the server as you like based on a query via ajax and then allows you to easily access it thorugh javascript once it’s returned…JSON is just a javascirpt object afterall.

:slight_smile:

Seconded. Espescially when using php, you can just create an associative php array and then use json_encode() to get easy, robust, and valid output.