使用 jQuery AJAX 删除数据库记录不起作用
我正在尝试从数据库中删除记录而不重新加载页面。在视图文件中我有以下代码。但是当我点击删除链接时什么也没有发生。请您帮我找出我做错了什么?
提前致谢。
我的视图文件:
<script type="text/javascript">
$(function(){ // added
$('a.delete').click(function(){
$.ajax({
var a_href = $('selector').attr('href');
type: "POST",
url: "<?php echo base_url(); ?>student_fee_status/payment_info_delete",
data: "id="+a_href,
success: function(html){
$("#show").html(html);
}
});
return false
});
}); // added
</script>
<?php if(count($records) > 0) { $i = 0; foreach ($records as $row){ $i++; ?>
<span> <?php echo $row['fee_type']; ?> : <?php echo $row['fee_amount']; ?> [<a id='<?php echo "$paymentid" ;?>'
class='delete' href='#'>Delete</a>]</span> <br>
<?php }} ?>
这是我的控制器:
function payment_info_delete(){
$id = mysql_real_escape_string($_POST['id']);//Some clean up :)
$query= $this->db->delete('studentpayment2', array('pid' => $id));
echo "$id";
}
I am trying to delete records from database without reloading the page. In the view file I have the following code. But when I click on the delete link nothing happens. Would you please kindly help me find out where I have done wrong?
Thanks in advance.
My view file:
<script type="text/javascript">
$(function(){ // added
$('a.delete').click(function(){
$.ajax({
var a_href = $('selector').attr('href');
type: "POST",
url: "<?php echo base_url(); ?>student_fee_status/payment_info_delete",
data: "id="+a_href,
success: function(html){
$("#show").html(html);
}
});
return false
});
}); // added
</script>
<?php if(count($records) > 0) { $i = 0; foreach ($records as $row){ $i++; ?>
<span> <?php echo $row['fee_type']; ?> : <?php echo $row['fee_amount']; ?> [<a id='<?php echo "$paymentid" ;?>'
class='delete' href='#'>Delete</a>]</span> <br>
<?php }} ?>
This is my Controller:
function payment_info_delete(){
$id = mysql_real_escape_string($_POST['id']);//Some clean up :)
$query= $this->db->delete('studentpayment2', array('pid' => $id));
echo "$id";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
var a_href...
”行放在ajax函数之外您需要将单词选择器替换为
this
,它指的是当前单击的元素。var a_href = $(this).attr('href');
$.ajax({...
var a_href...
' line outside of the ajax functionYou need to replace the word selector with
this
which refers to the currently clicked element.var a_href = $(this).attr('href');
$.ajax({...
正如 mgraph 在他的回答中指出的那样,
var a_href = $('selector').attr('href');
行不正确。您希望将'selector'
替换为this
,以便从单击的锚点获取 href 属性。也就是说,我认为这可能仍然是错误的 - 您肯定希望传回要删除的条目的 id,该 id 似乎存储在锚标记的 id 属性中,而不是始终传递返回
#
符号?要传回 id 而不是 href,请替换以下内容:
var a_href = $('selector').attr('href');
与此:
var a_href = this.id;
As mgraph pointed out in his answer, the
var a_href = $('selector').attr('href');
line is incorrect. You want to replace'selector'
withthis
so that you're getting the href attribute from the anchor that was clicked on.That said, I think that's still probably wrong - surely you want to be passing back the id of the entry to delete, which seems to be stored in the
id
attribute of the anchor tag, rather than always passing back a#
symbol?To pass the id back instead, rather than the href, replace this:
var a_href = $('selector').attr('href');
with this:
var a_href = this.id;