Ann
Ann

Reputation: 471

Ajax - compare the value returned by success function

I want to display a table row at the ajax request success.

    success : function(data)
                    {
                        if(data.status === "success")
                        {
                            $(parent_row).remove();
                            alert_box("success", "<?php echo 'keyword deleted successfully'; ?>", "#msg");

                       //display the table row
                            if(data.result == 0)
                            {
                                $("#no_result").show();
                            }
                        }

                    }

i want to compare the value returned by success function to be compared with an integer but the above code is not working i also want to display the table row having id "#no_result".

this is the console.log(data) result {"status":"success","result":0}

this is my controller function.

     public function delete_keywords()
{
    $this->load->model(array('Keywords','Keyword_topics'));
    $keyword = $this->input->post('keyword',TRUE);
    if(isset($keyword) && $keyword != '')
    {

        $this->Keywords->delete_records(array('kwd_id' => $keyword['k_id']));

        $this->Keyword_topics->delete_records(array('tkw_kwd_id' => $keyword['k_id']));
        $get_all_keywords = $this->Keywords->get_all_records();

        $count_ext_kwd=count($get_all_keywords);
        echo json_encode(array('status' => 'success','result' => $count_ext_kwd));

    }

}

I have faced a problem,i have a table (id, keywords, topics) containing inserted data,if no records is found in the table display "no records found".when insert only one(row) data ,then refresh the page,after delete the data..the message not display.

Upvotes: 0

Views: 1322

Answers (2)

Arun
Arun

Reputation: 146

You need to use parseJSON to parse the response data. To show the table row, first console '#no_result'. So that you can know whether it presents or not. Finally we can go for '.show()'. Because your element might not be presented.

success : function(data)
                    {
                        // add this line to your code 
                        parsedObj = jQuery.parseJSON(data); 
                        // use parsedObj below
                        if(parsedObj.status === "success")
                        {
                            $(parent_row).remove();
                            alert_box("success", "<?php echo 'keyword deleted successfully'; ?>", "#msg");

                       //display the table row
                            if(parsedObj.result == 0)
                            {
                                //before use '.show()' check whether element presents. 
                                 console.log("#no_result");
                                // If element presents use '.show()' else, find its parent and finally use '.show()'.  
                                //$("#no_result").show();
                            }
                        }

                    }

Upvotes: 1

mane
mane

Reputation: 1179

Then you could try something like this, hope it works

success : function(data)
                    {
                        var resObj = JSON.parse(data);
                        if(resObj.status === "success")
                        {
                            $(parent_row).remove();
                            alert_box("success", "<?php echo 'keyword deleted successfully'; ?>", "#msg");

                       //display the table row
                            if(resObj.result === 0)
                            {
                                $("#no_result").show();
                            }
                        }

                    }

Upvotes: 0

Related Questions