AleMal
AleMal

Reputation: 2087

Ajax, call multiple PHP function from a single js

i have a trouble with my project. In my site i have a page html with a single button and at onclick() eventa js function call intro.js, trough a XmlHttpRequestObject have to do many calls at many php function, in detail:

in js i call scan() function

function scan() {
 if (xmlHttp)
 {
 // try to connect to the server
 try
 {
  // initiate reading the async.txt file from the server
  xmlHttp.open("GET", "php/intro.php?P1=http://"+oStxt.value, true);
  xmlHttp.onreadystatechange = handleRequestStateChange;
  xmlHttp.send(null);
  // change cursor to "busy" hourglass icon
  document.body.style.cursor = "wait";    
}
// display the error in case of failure
catch (e)
{
  alert("Can't connect to server:\n" + e.toString());
  // revert "busy" hourglass icon to normal cursor
   document.body.style.cursor = "default";
}
}

}

And in handleRequestStatuschange i have:

function handleRequestStateChange()
 {
 // obtain a reference to the <div> element on the page
 // display the status of the request 
 if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4)
 {
 // revert "busy" hourglass icon to normal cursor
document.body.style.cursor = "default";
// read response only if HTTP status is "OK"
if (xmlHttp.status == 200) 
{
  try
  {
    // read the message from the server
    response = xmlHttp.responseText;
    // display the message 
document.body.appendChild(oRtag);
oPch = document.getElementById("divRtag");
oOch = document.createTextNode(response);
oPch.appendChild(oOch);
  }
  catch(e)
  {
    // display error message
    alert("Error reading the response: " + e.toString());
  }
} 
else
{
    // display status message
  alert("There was a problem retrieving the data:\n" + 
        xmlHttp.statusText);
  // revert "busy" hourglass icon to normal cursor
  document.body.style.cursor = "default"; 
}
}
}

It works for just one php call, but i need to call different php page in scan function after intro.php (scan2.php, scan3.php, ecc ecc) and with json_decode write single data of the array that return in div tags on my html page. Which is the best way to call different php pages and manage the results with a single js function in ajax?

Thanks in advance Alessandro

Upvotes: 0

Views: 818

Answers (2)

christopher
christopher

Reputation: 27346

Really, the simplest method that comes to mind is just to parameterise this function. This is as simple as

function doScan(url) { // Code here }

Then simply make the exact same ajax request with the url variable.

xmlHttp.open("GET", "php/" + url + "?P1=http://"+oStxt.value, true);

Next, simply call the doScan function with various parameters.

doScan("index.php");
doScan("otherPage.php");
doScan("somethingElse.php");

This will make ajax requests on the PHP file that you specify.

Upvotes: 0

DannyThunder
DannyThunder

Reputation: 1004

Not sure how you built your php-functions. Cant you create a function, that calls other functions (scans)?

function doScan(){

  $data = array();

 //like this, or with a loop
 $data['scan1'] = scan1();
 ....
 $data['scanN'] = scanN();

echo json_encode($data);

}

Upvotes: 2

Related Questions