dongmiao260399 2014-10-20 14:28
浏览 11
已采纳

使用按钮删除sql行而不刷新页面[关闭]

I want to delete a row from table with delete button with confirmation from user but without page refreshing with help of jQuery ajax.

I know a little about php, html, javascript, jquery, and sql. I dont know how to go about doing this but what i really want to do is to be able to pull data from a sql table (i already know how to do this).

Then display the data with a button next to it (i also already know how to do). When they click the button it will remove the element that hold the visible data and also send a sql statment to delete the row in the database. A very crude code over view.

<?php
echo '<p>Data 1: '".$someDataFromDB."'<button id='deleteThisRow'></button></p>;
?>

from this I would like to:

  1. Click the button
  2. remove the paragraph element so they users know it was deleted
  3. then send an sql statement to delete the row from the sql table

All this without refreshing the page. Is that at all possible?

  • 写回答

1条回答 默认 最新

  • douyun8885 2014-10-20 14:50
    关注

    Yes it is possible but you should only send the id of the entry to a particular page to delete the entry.never the entire sql statement.

    <button id="<?php echo $row['id']; ?>" class="delbutton"></button>
    

    add this script to your page

    <script type="text/javascript" >
            $(function() {
    
                $(".delbutton").click(function() {
                    var del_id = $(this).attr("id");
                    var info = 'id=' + del_id;
                    if (confirm("Sure you want to delete this post? This cannot be undone later.")) {
                        $.ajax({
                            type : "POST",
                            url : "delete_entry.php", //URL to the delete php script
                            data : info,
                            success : function() {
                            }
                        });
                        $(this).parents(".record").animate("fast").animate({
                            opacity : "hide"
                        }, "slow");
                    }
                    return false;
                });
            });
     </script>
    

    and last but no the least your delete script

    <?php
    include 'connection.php';
    $id=$_POST['id'];
    $delete = "DELETE FROM table WHERE id=$id";
    $result = mysql_query($delete) or die(mysql_error());
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭