[英]get data from a php page in a synchronous way
我有一個文件add.php
,它添加兩個數字並返回結果。 我的其他文件list.php
包含一個包含需要添加的數字的列表
$list = array ( 0=> 2,3
1=>4,5
2=>6,7);
這個數組可以更長,所以我必須用這樣的for
循環讀取它的數據:
for($i=0;$i<count($list);$i++) {
//something needs to be done here
}
我想要做的是添加所有對與一個提交按鈕。 我的問題是,只有前一個數字得到結果,才能添加每對數字。 我希望例如結果數組改變如下:
No1 | No2 | result
---- | ---- | --------
2 | 3 | on queue
---- | ---- | --------
4 | 5 | on queue
---- | ---- | --------
6 | 7 | on queue
No1 | No2 | result
---- | ---- | --------
2 | 3 | 5
---- | ---- | --------
4 | 5 | on queue
---- | ---- | --------
6 | 7 | on queue
No1 | No2 | result
---- | ---- | --------
2 | 3 | 5
---- | ---- | --------
4 | 5 | 9
---- | ---- | --------
6 | 7 | on queue
No1 | No2 | result
---- | ---- | --------
2 | 3 | 5
---- | ---- | --------
4 | 5 | 9
---- | ---- | --------
6 | 7 | 13
我一開始嘗試使用ajax,如下所示:
<script> function loadDoc(fid1,fid2) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById('content').innerHTML = xhttp.responseText;
}
};
xhttp.open('POST', 'play2.php', true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send('number1=$No1&number2=$No2');
}
</script>
其中$No1
, $No2
是$list[$i][0],$list[$i][1]
。 所以,這個失敗了,我想因為ajax的異步性質,但我沒有關於如何做到這一點的其他想法。 我想做的事情比這更復雜,但用這個例子來解釋它更容易。 如果可能的話,我想在沒有數據庫調用的情況下這樣做。
我設法使用ajax與async:false這樣做
<script>
$.ajax({
type: "POST",
url: "play2.php",
data: {number1:"'.$No1.'" , number2:"'.$No2.'"},
async: false,
success : function (data) {
$("#content").replaceWith(data);
}
});
</script>
我把它放在一個for循環中發送所有的值對,它按我的意願工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.