使用Codeigniter删除无需重新加载页面(3)

时间:2017-11-24 10:41:44

标签: codeigniter

在没有重新加载页面的情况下删除数据时出现问题。

我提请你注意我的观点:

    echo
'<table id="" border="0" cellpadding="4" cellspacing="0" > 
<tr>
<td >Text 1</td>
<td>Text 2</td>
<td>
<a href="' . base_url() . 'tournaments/tournaments_participant_delete/' . $id_tournament . '/' . $id_participant . '/' . $id_category . '" id="delete_participant_tournament" type="submit" class="btn btn-sm btn-danger pull-right" style="margin-right:5px;"> <span class="glyphicon glyphicon-remove"></span>
</a>
</tr>
</table>
<br>';

我提请你注意我的剧本:

    $(document).ready(function(){
    $("#delete_participant_tournament").click(function(event){
        var href = $(this).attr("href")
        var btn = this;
        $.ajax({
            type: "GET",
            url: href,
            success: function(response) {

                if (response == "Success")
                {
                    $(btn).closest('tr').fadeOut("slow");
                }
                else
                {
                    alert("Error");
                }
                return false;
            }
        });
        return false;
    });
});

当然还有我的控制器

public
function tournaments_participant_delete($tournament_id,$participant_id, $category)
{
    $this->tournaments_model->tournament_categories_participants_delete($tournament_id, $participant_id, $category);
    redirect('tournaments/add_participant_tournament/'.$tournament_id, 'refresh');
}

2 个答案:

答案 0 :(得分:0)

我注意到你可以删除的第一件事就是

type="submit"

注意到的其他事情,你正在进行AJAX调用。那么为什么在控制器中你会为同一个重定向?

redirect('tournaments/add_participant_tournament/'.$tournament_id, 'refresh');

您应该返回

之类的回复
echo "Success" or echo "Error" // Whatever be the state

更改它,它应该按照需要工作。

答案 1 :(得分:0)

尝试花些时间了解AJAX

HERE是使用jQuery AJAX的Codeigniter示例之一。

在您的代码中,在完成逻辑后避免从服务器端重定向。 即。而不是这个

redirect('tournaments/add_participant_tournament/'.$tournament_id, 'refresh');

你可以简单地将数据传递给视图,并在Jquery AJAX的成功函数中检索它

success: function(response) {
     // Here you can get the data passed by server
}

快乐编码......

相关问题