Php 在运行ajax删除行时,从datatable获取的数据始终来自第一行
嗨,我正在尝试使用jquery和Ajax从数据库中删除数据。每次单击“删除”按钮时,将获取第一行的数据。 这是我的ajax代码Php 在运行ajax删除行时,从datatable获取的数据始终来自第一行,php,jquery,ajax,Php,Jquery,Ajax,嗨,我正在尝试使用jquery和Ajax从数据库中删除数据。每次单击“删除”按钮时,将获取第一行的数据。 这是我的ajax代码 <script type="text/javascript"> $('#myTable').on('click','.b2',function() { if (confirm("Are you sure?")) { var id = $('.b2').val(); alert(id);
<script type="text/javascript">
$('#myTable').on('click','.b2',function()
{
if (confirm("Are you sure?"))
{
var id = $('.b2').val();
alert(id);
var dataString = {id : id };
$.ajax({
type: "POST",
url: "ajax_delete.php",
data: dataString,
success: function()
{
alert("datas are successfully deleted..");
},
error: function()
{
alert("Error occured..");
}
});
}
});
$('#myTable')。在('单击','.b2',函数()上
{
如果(确认(“你确定吗?”)
{
var id=$('.b2').val();
警报(id);
var-dataString={id:id};
$.ajax({
类型:“POST”,
url:“ajax_delete.php”,
数据:dataString,
成功:函数()
{
警报(“数据已成功删除…”);
},
错误:函数()
{
警报(“发生错误…”;
}
});
}
});
这是我的php文件
<?php
$mysql_host = 'localhost';
$mysql_database = 'proj1';
$mysql_user = 'root';
$mysql_password='';
$mysqli = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
if (mysqli_connect_errno($mysqli))
{
echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
$id=$_POST['id'];
$delete = "delete from regestration_details where eid=".$id."";
$ss=mysqli_query( $mysqli,$delete);
?>
这是我的主页
<form name="f1" method="post">
<div class="table-responsive">
<table id="myTable" class="display table" style="overflow:auto;" width="80%" >
<thead><tr><th>Employee ID</th><th>Name</th><th>Desigation</th><th>Blood Group</th><th>Address</th><th>Contact Number</th><th></th><th></th></tr></thead>
<?php
$mysql_host = 'localhost';
$mysql_database = 'proj1';
$mysql_user = 'root';
$mysql_password='';
$mysqli = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
if (mysqli_connect_errno($mysqli))
{
echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
$result=mysqli_query($mysqli,"select * from regestration_details");
if (mysqli_num_rows($result) > 0)
{
// echo "<table cellpadding=10 border=4 background-color:#7E3A18>";
// echo "<th>" Employee ID "</th>";
while($row = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
echo"<td><input type='button' name='b1' class='b1' id='bt1' value=".$row[0]." class='btn btn-primary btn-lg btn-block' style='background-color:#55AA00;color:white;height:9%;width:90%;border:0;'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></td>";
echo"<td><input type='button' name='b2' class='b2' id='bt2' value=".$row[0]." class='btn btn-primary btn-lg btn-block' style='background-color:#D50000;color:white;height:9%;width:90%;border:0;'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No rows found!";
}
?>
</table>
</div>
</form>
员工ID姓名DesigationBlood组地址联系电话
问题出在选择器中:
var id = $('.b2').val();
当您使用classb2
获取所有输入时
要仅获取单击输入的值,请将其更改为:
$(this).val();
它应该会起作用