使用 jQuery AJAX 删除数据库记录不起作用

发布于 2024-12-21 05:30:25 字数 1239 浏览 2 评论 0原文

我正在尝试从数据库中删除记录而不重新加载页面。在视图文件中我有以下代码。但是当我点击删除链接时什么也没有发生。请您帮我找出我做错了什么?

提前致谢。

我的视图文件:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

維他命╮ 2024-12-28 05:30:25
  1. 您需要将“var a_href...”行放在ajax函数之外
  2. 您需要将单词选择器替换为this,它指的是当前单击的元素。

    var a_href = $(this).attr('href');
    $.ajax({...

  1. You need to put the 'var a_href...' line outside of the ajax function
  2. You need to replace the word selector with this which refers to the currently clicked element.

    var a_href = $(this).attr('href');
    $.ajax({...

陌上芳菲 2024-12-28 05:30:25
var a_href = $('selector').attr('href');
$.ajax({...
var a_href = $('selector').attr('href');
$.ajax({...
寄离 2024-12-28 05:30:25

正如 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' with this 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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文