简体   繁体   English

PHP使用ajax刷新表格内容

[英]PHP Refresh table content using ajax

I have a form which contains a table which is populated with data dynamically according to selected option in dropdown. 我有一个表单,其中包含一个表,该表根据下拉列表中的选定选项动态填充数据。

The dropdown contains 2 values "enabled" and "disabled". 下拉列表包含2个“启用”和“禁用”值。 The first column of each row in table contains a checkbox. 表中每行的第一列包含一个复选框。 The table structure is as follows. 表结构如下。

[checkbox ]| user | status

[checkbox1]| jim  | enabled

[checkbox2]| sam  | disabled

The checkbox value is equal to userid. 复选框值等于userid。

There is a button for changing the status of selected users. 有一个用于更改所选用户状态的按钮。

on button click the selected checkbox value are posted using ajax and status of selected users are changed but the data is only refreshed on page reload. 在按钮上单击所选复选框值将使用ajax发布,并且所选用户的状态已更改,但数据仅在页面重新加载时刷新。

How can I refresh the table when the status is changed. 如何在状态更改时刷新表。 Here is my script. 这是我的剧本。

 function Status(){
    var checked = []
    $("input[name='select[]']:checked").each(function ()
    {
        checked.push(parseInt($(this).val()));
    });
    if(checked!=''){

                    $.ajax({
                        type:'post',
                        url:site_url()+'/common/changeStatus',
                        data:{'checked':checked},
                        dataType:'json',
                        async:false,
                        success:function(result){

                            if(result!= "false"){
                                $.msgBox({
                                    title:"Success",
                                    content:"Status change successful"
                                });
                                $(function () {
                                    $('.msgButton').click(function (event) {
                                        event.preventDefault();
                                        $("#table").load($(this).attr("#table"));

                                    });
                                });                                
                            }

Here #table is the id of the table containig the data. 这里#table是包含数据的表的id。

You have to construct the table with current status in PHP file itself. 您必须在PHP文件本身中构造具有当前状态的表。 Then you can send it as response. 然后你可以发送它作为回应。 In the Jquery AJAX response you have to add like this, 在Jquery AJAX响应中你必须像这样添加,

$(".table").html(resultTable);

resultTable is the response you are passing from PHP file. resultTable是您从PHP文件传递的响应。 If you do like this only, you can get the table with the updated statuses. 如果您只是这样做,您可以获得具有更新状态的表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM