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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵