php 使用ajax函数和php删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19724383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:56:05  来源:igfitidea点击:

delete row with ajax function and php

phpjquerymysqlajax

提问by Hamed mayahian

i have a table with mysql Data,i add a trash button and i want remove each row when trash button is clicked with ajax function, this is my html:

我有一个包含 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>

and my ajax function here:

和我的 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")
                            }
                    }

                })
            })
    });

and also my "delete.php" page here:

还有我的“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);

?>

i think my problem is ajax function; thanks

我想我的问题是ajax函数;谢谢

回答by roullie

try this

尝试这个

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

     })
})

and also change

并且也改变

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

to

$music_number = $_POST['del_id'];

回答by Chris Pickford

In addition to the above answers, you should delegate your on click handler to prevent unnecessary duplication

除了上述答案之外,您还应该委托您的点击处理程序以防止不必要的重复

$(document).on('click', '.trash', function() { ... });

回答by Code L?ver

Your ajax code should be this:

你的ajax代码应该是这样的:

$(function(){
    $(document).on('click','.trash',function(){
        var del_id= $(this).attr('id');
        var $ele = $(this).parent().parent();
        $.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")
                 }
             }

            });
        });
});

And PHP code should be:

而 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);
if(isset($result)) {
   echo "YES";
} else {
   echo "NO";
}
?>

回答by Anand Solanki

Try this:

尝试这个:

$music_number = POST['del_id']; in delete.php

write ajax function like:

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

                })
            });
  • Thanks
  • 谢谢

回答by jagmohan

Here are the things that you need to correct

以下是你需要纠正的事情

  • In "delete.php" file $music_number = "POST['del_id']"; // to $music_number = $_POST['del_id'];

    Also, in the success callback of ajax, you are checking for "YES" in response which is not sent anywhere in this file.

  • Change to your ajax request

    data: {'del_id':del_id},

  • 在“delete.php”文件中 $music_number = "POST['del_id']"; // to $music_number = $_POST['del_id'];

    此外,在 ajax 的成功回调中,您正在检查响应中未发送到此文件中的任何位置的“YES”。

  • 更改为您的 ajax 请求

    data: {'del_id':del_id},

Hope this helps.

希望这可以帮助。

回答by bipen

send you data as object and not just value

将数据作为对象发送给您,而不仅仅是值

 ...
 type:'POST',
 url:'delete.php', 
 data:{'del_id':del_id},  //<----here
 ....

and take it as POST in delete.php

并将其作为 delete.php 中的 POST

 $music_number = $_POST['del_id'];

updated

更新

add this to your delete.php

将此添加到您的 delete.php

<?php
 include('../db_inc.php');
 $music_number = $_POST['del_id'];
  $qry = "DELETE FROM music WHERE msuic_id ='$music_number'";
  $result=mysql_query($qry);
  if($result) {
      echo "Yes";
   } else {
      echo "No";
   }
 ?>