我正在尝试创建一个像 fb 这样的 friend 按钮。我有一个 while 循环,它会回显名称和一些其他数据,我需要将每个用户请求分别保存到数据库中的表中。我的问题是我不'不知道如何用ajax传递它们,因为我在文本框按钮上有相同的id。我尝试用相同的id传递它们,但这传递了3次相同的数据,或者只传递了其中一个。所以我尝试使用id =“something$” row[id]"来定义每个文本框和按钮,但我不知道如何触发 jquery 函数并使用 ajax 发布它们。 a picture of what i am trying
my php file
echo "<div class='col-xs-12'>
<div class='row fixed-table'>
<div class='table-content'>
<table class='table table-striped text-muted' id='mytable'>
<tbody>";
while($row = mysqli_fetch_array($requests)){
$username=("SELECT * FROM users WHERE user_id=".$row['patient']);
$found=mysqli_fetch_array(mysqli_query($sql_link,$username));
echo "<tr>
<td class='text-center'>
<p>
$found[username]
<p>
</td>
<td class='text-center'>";
$patient_username=$found['username'];
$patient_id=$found['user_id'];
echo"<input type='text' id='patient_id$row[id]' class='hidden' value='$patient_id'>
<input type='text' id='patient_username$row[id]' class='hidden' value='$patient_username'>
<input type='text' id='doctor_id$row[id]' class='hidden' value='$doctor_id'>
<input type='text' id='doctor_fname$row[id]' class='hidden' value='$doctor_fname'>
<input type='button' id='accept_btn$row[id]' class='btn btn-primary' value='Accept'>
</td>
</tr>";
}
echo "</tbody>
</table>
</div>
</div>
</div>";
my js file
function decision(){
var patient_id = $('#patient_id').val();
var patient_username = $('#patient_username').val();
var doctor_id = $('#doctor_id').val();
var doctor_fname = $('#doctor_fname').val();
$.ajax({
type:"post",
url:"php/add.php",
data: {"patient_id": patient_id,"patient_username": patient_username,"doctor_id": doctor_id,"doctor_fname": doctor_fname},
success:function(data){
$("#decision").html(data);
}
});
$('#accept_btn').click(function(){
decision();
});
最佳答案
这对于 JQUERY 来说很容易,因为您甚至不需要知道表单中的 ID。然而,首先您需要将所有输入包装到一个表单中,并为该表单分配一个 ID,并为每个输入分配一个名称。
var data = $("#myForm").serialize();
$.post('myPHPFILEURL.php', data,function(retData){
//CODE THAT HANDLES SERVER RESPONSE
});
echo "<div class='col-xs-12'>
<div class='row fixed-table'>
<div class='table-content'>
<form id='myForm'>
<table class='table table-striped text-muted' id='mytable'>
<tbody>";
while($row = mysqli_fetch_array($requests)){
$username=("SELECT * FROM users WHERE user_id=".$row['patient']);
$found=mysqli_fetch_array(mysqli_query($sql_link,$username));
echo "<tr>
<td class='text-center'>
<p>
$found[username]
<p>
</td>
<td class='text-center'>";
$patient_username=$found['username'];
$patient_id=$found['user_id'];
echo"<input type='text' name='patient_id$row[id]' id='patient_id$row[id]' class='hidden' value='$patient_id'>
<input type='text' id='patient_username$row[id]' name='patient_username$row[id]' class='hidden' value='$patient_username'>
<input type='text' id='doctor_id$row[id]' name='doctor_id$row[id]' class='hidden' value='$doctor_id'>
<input type='text' id='doctor_fname$row[id]' name='doctor_fname$row[id]' class='hidden' value='$doctor_fname'>
<input type='button' id='accept_btn$row[id]' name='accept_btn$row[id]' class='btn btn-primary' value='Accept'>
</td>
</tr>";
}
echo "</tbody>
</table>
</form>
</div>
</div>
</div>";
只需选择您的表单并序列化()数据,然后将新的序列化字符串传递到 AJAX 函数的数据参数中。
关于javascript - 如何使用ajax从php while循环发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33380588/