php 如何使用ajax删除记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19446029/
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:29:03  来源:igfitidea点击:

How to delete record using ajax?

javascriptphpjquerymysqlajax

提问by Jahaan

I am trying to delete record from database using AJAX. The confirmation window does not appear so that the record can be deleted. here is the code..

我正在尝试使用 AJAX 从数据库中删除记录。不会出现确认窗口,因此可以删除记录。这是代码..

<?php
$q = $_GET['q'];
$p = $_GET['p'];
$sql="SELECT * FROM course_details WHERE sem='" . $q . "' AND branch='" . $p . "' ORDER BY course_codes ASC";
$result = mysql_query($sql);        
while($row = mysql_fetch_assoc($result)){
  echo '<tr class="record">';
  echo "<td>" . $row['course_codes'] . "</td>";
  echo "<td>" . $row['course_names'] . "</td>";
  echo "<td>" . $row['course_instructors'] . "</td>";
  echo "<td>" . $row['course_credits'] . "</td>";
  echo '<td><div align="center"><a href="#" id="' . $row['course_id'] . '" class="delbutton" title="Click To Delete">delete</a></div></td>';
  echo '</tr>';
}
echo "</table>";
mysql_close($bd);
?>

Here $p and $q are send by an AJAX script from another page. It is working fine. The records are displayed as expected. Deletion works using AJAX if i do not use AJAX to display records.The script I am using to delete is:

这里 $p 和 $q 是由另一个页面的 AJAX 脚本发送的。它工作正常。记录按预期显示。如果我不使用 AJAX 来显示记录,则使用 AJAX 进行删除。我用来删除的脚本是:

<script src="jquery.js"></script>
<script type="text/javascript">
$(function() {
$(".delbutton").click(function(){
var element = $(this);
var del_id = element.attr("id");
var info = 'id=' + del_id;
if(confirm("Are you sure you want to delete this Record?")){
    $.ajax({
        type: "GET",
        url: "deleteCourse.php",
        data: info,
        success: function(){   
    }
});
    $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
    .animate({ opacity: "hide" }, "slow");
}
return false;
});
});
</script>

deleteCourse.php

删除课程.php

if($_GET['id']){
    $id=$_GET['id'];
    $id = mysql_escape_string($id);
}
$del = "DELETE from course_details where course_id = '$id'";

$result = mysql_query($del);

回答by SarathSprakash

The problem is because you are creating dynamic elements so you have to use a delagate $(document).on()inorder to bind the click event to the elements. Here is the corrected code

问题是因为您正在创建动态元素,因此您必须使用 delagate $(document).on()才能将 click 事件绑定到元素。这是更正后的代码

<script type="text/javascript">
        $(function() {
        $(document).on('click','.delbutton',function(){
        var element = $(this);
        var del_id = element.attr("id");
        var info = 'id=' + del_id;
        if(confirm("Are you sure you want to delete this Record?")){
            $.ajax({
                type: "GET",
                url: "deleteCourse.php",
                data: info,
                success: function(){  } 
            });
        }
        return false;
        });
        });
    </script>

and your deletCourse.php

和你的deletCourse.php

if($_GET['id']){
    $id=$_GET['id'];
    $id = mysql_escape_string($id);
}
$del = "DELETE from course_details where course_id = ".$id."";

$result = mysql_query($del);

Hope this helps, Thank you

希望这有帮助,谢谢

回答by Mustafa Ehsan Alokozay

Try this:

尝试这个:

<script type="text/javascript" src="jquery.js"></script>

回答by Arun Kumar

try this one

试试这个

 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>

 <a href="#" id="1" onclick="del(this.id);return false;">delete</a>
 <a href="#" id="2" onclick="del(this.id);return false;">delete</a>
 <a href="#" id="3" onclick="del(this.id);return false;">delete</a>
 <a href="#" id="4" onclick="del(this.id);return false;">delete</a>
 <a href="#" id="5" onclick="del(this.id);return false;">delete</a>

javascript

javascript

function del(id)
{
 var info = 'id=' + id;
    if(confirm("Are you sure you want to delete this Record?")){
        var html = $.ajax({
        type: "POST",
        url: "delete.php",
        data: info,
        async: false
        }).responseText;

        if(html == "success")
        {
            $("#delete").html("delete success.");
            return true;

        }
        else
        {
            $("#captchaStatus").html("incorrect. Please try again");

            return false;
        }
    }
}

ajax file

ajax文件

if($_GET['id']){
$id=$_GET['id'];
$id = mysql_escape_string($id);
}
$del = "DELETE from course_details where course_id = '$id'";
$result = mysql_query($del);
if($result)
{
echo "success";
}

回答by suresh

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function del(id)
{

 var info = 'id=' + id;
    if(confirm("Are you sure you want to delete this Record?")){
        var html = $.ajax({
        type: "GET",
        url: "deletCourse.php",
        data: info,
        async: false ,
         success: function() {
    window.location.reload(true);}
        }).responseText;


    }
}
</script>

<?php
$link=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("cart");
$sql=mysql_query("SELECT * FROM `details`");
echo "<table>";
echo "<tr><th>Name</th><th>NO of Items</th></tr>";
while($row = mysql_fetch_assoc($sql)){
  echo '<tr class="record">';
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['num'] . "</td>";

  echo '<td><div align="center"><a href="#"  id="' . $row['id'] . '" onclick="del('.$row['id'].')">delete</a></div></td>';
  echo '</tr>';
}
echo "</table>";
mysql_close($link);
?>