douhu4091
2013-11-04 11:21
浏览 89

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

图片转代码服务由CSDN问答提供 功能建议

我的项目出了问题。 在我的网站上我有一个带有单个按钮的页面html并且在onclick ()eventa js函数调用intro.js,通过一个XmlHttpRequestObject必须在很多php函数上做很多调用,详细说明:

在js中调用scan()函数 \ n

  function scan(){
 if if(xmlHttp)
 {
 //尝试连接服务器
尝试
 {
 //启动读取async.txt文件 来自服务器
 xmlHttp.open(“GET”,“php / intro.php?P1 = http://”+ oStxt.value,true); 
 xmlHttp.onreadystatechange = handleRequestStateChange; 
 xmlHttp.send(null  ); 
 //将光标更改为“busy”沙漏图标
 document.body.style.cursor =“wait”;  
} 
 //在出现故障时显示错误
catch(e)
 {
 alert(“无法连接到服务器:
”+ e.toString()); 
 //还原 “忙”沙漏图标到正常光标
 document.body.style.cursor =“default”; 
} 
} 
 
} 
   
 
 

在handleRequestStatuschange中我有:

  function handleRequestStateChange()
 {
 //获取对&lt; div&gt;的引用 页面上的元素
 //显示请求的状态
 if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4)
 {
 //将“忙”沙漏图标恢复为正常光标\  ndocument.body.style.cursor =“default”; 
 //仅当HTTP状态为“OK”时读取响应
if(xmlHttp.status == 200)
 {
 try 
 {
 //读取 来自服务器的消息
 response = xmlHttp.responseText; 
 //显示消息
document.body.appendChild(oRtag); 
oPch = document.getElementById(“divRtag”); 
oOch = document.createTextNode(response  ); 
oPch.appendChild(oOch); 
} 
 catch(e)
 {
 //显示错误消息
 alert(“读取响应时出错:”+ e.toString()); 
  } 
} 
else 
 {
 //显示状态消息
 alert(“检索数据时出现问题:
”+ 
 xmlHttp.statusText); 
 //恢复“忙”沙漏图标 到普通光标
 document.body.style.cursor =“default”;  
} 
} 
} 
   
 
 

它适用于一个php调用,但我需要在intro.php后调用扫描函数中的不同php页面( scan2.php,scan3.php,ecc ecc)和json_decode写入我的html页面上div标签返回的数组的单个数据。 这是调用不同php页面并使用单个js函数管理结果的最佳方法 在ajax?

提前致谢 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);
    
    }
    
    已采纳该答案
    打赏 评论
  • dongyuduan1890 2013-11-04 11:28

    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.

    打赏 评论

相关推荐 更多相似问题