Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Specfic response from MySql in jQuery AJAX Success

Well i have this ajax code which will return the result from MySql in Success block.

$.ajax({
   type:"POST",
   url:"index.php",
   success: function(data){
            alert(data);                
        }
});

My Query

$sql = "SELECT * FROM tablename";
$rs=parent::_executeQuery($sql);
$rs=parent::getAll($rs);
print_r($rs);
return $rs

My Response Array in alert of Success AJAX

Array
(
    [0] => Array
        (
            [section_id] => 5
            [version] => 1
            [section_name] => Crop Details
            [id] => 5
            [document_name] => Site Survey
            [document_master_id] => 1
            [document_section_id] => 5
        )

    [1] => Array
        (           
            [section_id] => 28
            [version] => 1
            [section_name] => Vegetative Report           
            [id] => 6
            [document_name] => Site Survey
            [document_master_id] => 1
            [document_section_id] => 28
        )

)

I want to get only section_name and document_name from the result so that i can append these two values to my list.

like image 366
Matarishvan Avatar asked Dec 15 '14 06:12

Matarishvan


People also ask

How do I return data after ajax call success?

You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });

How check ajax request is successful jQuery?

$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });


1 Answers

Don't return the response using print_r(), use json_encode():

echo json_encode($rs);

Then in the Javascript, you can do:

$.ajax({
   type:"POST",
   url:"index.php",
   dataType: 'json'
   success: function(data){
        for (var i = 0; i < data.length; i++) {
            console.log(data[i].section_name, data[i].document_name);          
        }
    }
});
like image 185
Barmar Avatar answered Oct 03 '22 22:10

Barmar