小编典典

用ajax功能和php删除行

ajax

我有一个带有mysql数据的表,我添加了一个垃圾桶按钮,我想用ajax函数单击垃圾桶按钮时删除每一行,这是我的html:

  <table border="1">
    <?php
$sql ="SELECT * FROM music";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_object($result)){

    echo '<tr><td>'.$row->file_name.'</td><td>'.$row->composer.'</td><td>'.$row->lyric.'</td><td>'.$row->music_thumb.'</td><td>'.'

    <a href="#" id="'.$row->msuic_id.'" class="trash" >
    جذف کردن
    </a>

    '.'</td></tr>';
    }

?>
  </table>

和我的ajax功能在这里:

$(function(){
        $('.trash').click(function(){
            var del_id= $(this).attr('id');
            var $ele = $(this).parent().parent();
            $.ajax({
                type:'POST',
                url:'delete.php',
                data:del_id,
                success: function(data){
                    if(data=="YES"){
                        $ele.fadeOut().remove();
                        }else{
                            alert("can't delete the row")
                            }
                    }

                })
            })
    });

还有我的“ delete.php”页面:

<?php
include('../db_inc.php');
$music_number = "POST['del_id']";
echo '$music_number';
$qry = "DELETE FROM music WHERE msuic_id ='$music_number'";
$result=mysql_query($qry);

?>

我认为我的问题是ajax函数;谢谢


阅读 282

收藏
2020-07-26

共1个答案

小编典典

试试这个

$.ajax({
    type:'POST',
    url:'delete.php',
    data:{del_id:del_id},
    success: function(data){
         if(data=="YES"){
             $ele.fadeOut().remove();
         }else{
             alert("can't delete the row")
         }
    }

     })
})

并且也改变

$music_number = "POST['del_id']";

$music_number = $_POST['del_id'];
2020-07-26