我正在尝试使用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>
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);
答案 0 :(得分: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>
和你的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);
希望这有帮助,谢谢
答案 1 :(得分:0)
试试这个:
<script type="text/javascript" src="jquery.js"></script>
答案 2 :(得分:0)
尝试这个
<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文件
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";
}
答案 3 :(得分: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);
?>