1

我正在尝试使用 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);
?>

这里 $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>

删除课程.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);
4

4 回答 4

3

问题是因为您正在创建动态元素,因此您必须使用 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>

和你的 deleteCourse.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);

希望这会有所帮助,谢谢

于 2013-10-18T09:37:35.357 回答
1

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

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

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";
}
于 2013-10-18T14:41:17.193 回答
0
<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);
?>
于 2015-01-18T15:11:49.463 回答
0

尝试这个:

<script type="text/javascript" src="jquery.js"></script>
于 2013-10-18T09:48:11.090 回答