我试图删除一行而不改变页面

时间:2014-12-15 10:59:35

标签: php jquery ajax

<?php
          while ($row = mysqli_fetch_array($query,MYSQLI_BOTH)) 
          {
            $id= $_POST['tran_no'];;
        ?>

              <tbody>
                  <tr id=<?php echo $row['trans_no']?>>
                    <td><?php echo $row['trans_no']?></td>
                    <td><?php echo $row['obj_code']?></td>
                    <td><?php echo $row['div_no']?></td>
                    <td><?php echo $row['check_no']?></td>
                    <td><?php echo $row['payee']?></td>
                    <td><?php echo $row['payment']?></td>
                    <td><?php echo $row['add']?></td>
                    <td><?php echo $row['amount']?></td>
                    <td><?php echo $row['amountw']?></td>
                    <td>
                      <a href="#" id="<?php echo $id; ?>" class="delete" title="Delete"><img src="image/remove.png"></a>
                    </td>
                  </tr>

        <?php
      }
      ?>

                    </tbody>
                </table>
            </div>
      <script src="jquery.js"></script>
      <script type="text/javascript">
      $(function() {
      $(".delete").click(function(){
      var element = $(this);
      var del_id = element.attr("trans_no");
      var info = 'trans_no=' + del_id;
      if(confirm("Are you sure you want to delete this?"))
        {
          $.ajax({
          type: "POST",
          url: "delete.php",
          data: info,
          success: function(){
        }
        });
          $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
          .animate({ opacity: "hide" }, "slow");
        }
      return false;
        });
        });
      </script>

我的问题是ajax代码似乎没有删除行,我不知道是否执行了ajax代码。帮助将是非常需要的,请让我深入了解我的代码有什么问题,或者我是否需要添加一些东西。继承了delete.php

<?php
$con = mysqli_connect("localhost","root"," ","dole") or die("Could not connect database");
if($_POST['trans_no'])
{
$id=$_POST['trans_no'];
$delete = "DELETE FROM table_no WHERE trans_no = '$id'";
$res=mysqli_query($con,$delete);
}

?>

3 个答案:

答案 0 :(得分:0)

选项1:

您正在将数据库中的ID分配给id属性,并在AJAX中,阅读trans_no

trans_no属性未设置为删除链接。

这就是为什么你没有得到它。

将您的HTML更改为

<a href="#" trans_no="<?php echo $id; ?>" class="delete" title="Delete"><img src="image/remove.png"></a>

使其符合HTML5标准:

<a href="#" data-id="<?php echo $id; ?>" class="delete" title="Delete"><img src="image/remove.png"></a>

改变JS:

var del_id = element.attr("data-id");

选项2

只需更改一行:

var del_id = element.attr("trans_no");

var del_id = element.attr("id");

并使用您现有的代码。

它会起作用。

答案 1 :(得分:0)

试试这个:

$(function() {
  $(".delete").click(function(){
     var element = $(this);         
     var transNoVar = element.attr('id');

     if(confirm("Are you sure you want to delete this?"))
     {
         $.ajax({
            type: "POST",
            url: "delete.php",
            data: {
                trans_no: transNoVar
            },
            success: function(data){
                // delete.php return 1 on success 0 otherwise, check it here
                if(data=='1') {
                    // on success hide the tr
                    // add class="show" to the tr
                    $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
      .animate({ opacity: "hide" }, "slow");
                } else {
                    alert('Error deleting your record');
                }
            }
        });          
    }
    return false;
  });
});

答案 2 :(得分:0)

尝试以下代码,

$(".delete").click(function(){
   var element = $(this);
   var del_id = this.id;// use this.id as your database id related to element id
   var info = {trans_no : del_id};
   if(confirm("Are you sure you want to delete this?")){
      $.ajax({
        type: "POST",
        url: "delete.php",
        data: info,
        success: function(data){
            if(data=='success'){
                 // hide only in case of successful deltion
                 element.parents(".show").animate({ backgroundColor: "#003" }, "slow")
                  .animate({ opacity: "hide" }, "slow");
            } else {
                alert('Error while deleting');
            }
        }
      });          
   }
   return false;
});

在PHP中返回successerror之类的,

<?php
  $con = mysqli_connect("localhost","root"," ","dole") or die("Could not connect database");
  $status='error';
  if(isset($_POST['trans_no']) && $_POST['trans_no']){
     $id=$_POST['trans_no'];
     $delete = "DELETE FROM table_no WHERE trans_no = '$id'";
     $res=mysqli_query($con,$delete);
     if($res){// only success in case of deleted.
        $status='success';
     }
  }
  echo $status;
?>