Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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". 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.

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.

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.

like image 636
Avinash Avatar asked Nov 04 '22 04:11

Avinash


1 Answers

You have to construct the table with current status in PHP file itself. Then you can send it as response. In the Jquery AJAX response you have to add like this,

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

resultTable is the response you are passing from PHP file. If you do like this only, you can get the table with the updated statuses.

like image 98
Edwin Alex Avatar answered Nov 09 '22 15:11

Edwin Alex