douhu4091 2013-11-04 11:21
浏览 112
已采纳

Ajax,从单个js调用多个PHP函数

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:
" + 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:
" + 
        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

  • 写回答

2条回答 默认 最新

  • drnx3715 2013-11-04 11:25
    关注

    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);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况