在运行ajax以删除行时,从数据表获取的数据始终来自第一行

时间:2015-06-25 12:54:13

标签: php jquery ajax

您好我正在尝试使用j查询和Ajax从数据库中删除数据。每次单击删除按钮时,将从第一行获取数据。 这是我的ajax代码

<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..");   
        }
        });
    }
    });

这是我的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>

请帮忙吗?

1 个答案:

答案 0 :(得分:5)

问题在于您的选择器:

var id = $('.b2').val();

当您获得课程b2的所有输入时。

要获得所点击输入的值,请将其更改为:

$(this).val();

它应该有效