【发布时间】:2014-03-13 11:20:10
【问题描述】:
我这里有个小问题。我想从 PHP foreach 循环中调用 Ajax 函数 这是我的代码:
阿贾克斯:
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
var bla = document.getElementById('bla').value;
var queryString = "?bla=" + bla;
ajaxRequest.open("GET", "ajax-example.php" +
queryString, true);
ajaxRequest.send(null);
}
还有php代码:
foreach ($data AS $key => $value){
<form name="myForm">
<input type="text" id="bla" name="bla" value="<?php echo $value['bla']; ?>">
<input type="button" onclick="ajaxFunction(<?php echo $value['id']; ?>)" value="Speichern">
</form>
}
问题:当我单击 foreach 循环中生成的几个按钮时,只有 foreach 循环的第一个结果出现在输出中。我想我必须将 foreach 循环(来自数据库)中的单个 ID 提交到Ajax 函数,但我不知道该怎么做。
也许像这样调用函数?函数 ajaxFunction(id) ?!
【问题讨论】: