gpt4 book ai didi

javascript - 我得到每个按钮的相同值

转载 作者:行者123 更新时间:2023-11-30 19:28:11 25 4
gpt4 key购买 nike

我已经从数据库中获取数据,一切正常,但问题是当我向 test.php 提交 ajax 请求时,每个按钮的值都相同我对 Ajax 和 Java 很陌生,所以请帮助我,我很困惑如何分别获取每个按钮的值并提交到 test.php 文件

<tbody>
<?php
$letter = mysqli_query($con,"SELECT * FROM letters order by id DESC");

if (mysqli_num_rows($letter) > 0) {
while ($rows_letter=mysqli_fetch_array($letter)) {
$id = $rows_letter['id'];
$subject = $rows_letter['subject'];
$status = $rows_letter['status'];
?>

<tr>
<th class="text-center" scope="row">1</th>
<td class="text-center"><?php echo $subject ;?></td>
<td class="text-center">
<?php
if ($status == 1) {
echo '<mark style="background-color: #5cb85c; color:white;"> Successfully Sent </mark>';
} else {
echo '<mark style="background-color:#f0ad4e; color:white;"> Not Sent Yet </mark>';
}
?>
</td>
<td>
<button type="button" class="btn btn-info btn-sm btn-block">
<span class="fa fa-pencil-square-o"></span> Edit</button>
</td>
<td>
<button type="button" class="btn btn-danger btn-sm btn-block">
<span class="fa fa-trash-o"></span> Move To Trash</button>
</td>
<td>
<button type="button" onclick="startsend();" id="id" value="<?php echo $id;?>"class="btn btn-success btn-sm btn-block">
<span class="fa fa-paper-plane-o"></span> Send To All</button>
</td>
</tr>
<?php
}
}
?>
</tbody>
<script type='text/javascript'>

//AJAX function
function startsend() {
var id = $('#id').val();
$.ajax({
type: "POST",
url: "test.php",
data:{ id: id
},
success: function(msg){
alert( "Button Id is " + msg );
}
});
}
</script>

这是我的 test.php 文件

<?php 
$id = $_POST['id']; echo $id;
//// rest of process according to id
?>

最佳答案

试试这个,将 id 作为 param 传递给 ajax

HTML:

<td><button type="button" onclick="startsend(<?php echo $id;?>);"
id="id" value="<?php echo $id;?>"class="btn btn-success btn-sm btn-block">
<span class="fa fa-paper-plane-o"></span> Send To All</button></td>

Ajax :

function startsend(id) {
$.ajax({
type: "POST",
url: "test.php",
data:{ id: id },
success: function(msg){
alert( "Button Id is " + msg );
}
});
}

关于javascript - 我得到每个按钮的相同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56698379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com