How to get array data from database query in php file to ajax sucess data

I am trying to get the data that is in an array from SQL query to ajax success data. Here is the code I tried,

function adminchecksub(value1){
//alert('hi');
var value1=$('#adminsid').val();
//alert(value1);
if(value1==''){
alert('Please enter a Sub id');
}
else{
//alert(value1);
 $.ajax({

                                        type: 'POST',
                                        url: root_url + '/services/services.php?method=adminsubcheck',
                                        data: {value1:value1},
                                        async: true,
                                        success: function (data) {
                                                alert(data);

                                                if (data == 1) {
                                                        alert('inside');
                                                        //window.location.assign(rdurl+"?sid="+sid);
                                                        return true;
                                                } else {
                                                        //alert('does not exist!');
                                                       //alert('outside');
                                                        return false;
                                                }

                                        }
                                });
}

      

now in the php method that is requested from the above ajax call i need to return data with this function which returns an array

   function adminsubcheck($sid){
$subid=$sid['value1'];
$row = array();
//echo $subid;
$query = "SELECT Sub_id,Status,Sub_type FROM Sub WHERE Sub_id=$subid";        
        //echo $query;
        //echo $subid;
        $queryresult = mysql_query($query);
        $count  = mysql_num_rows($queryresult);
         //echo $count;

         while ($r = mysql_fetch_assoc($queryresult)) {

           $row[] = $r;
        } 
        return $row;

}

      

here $ row returns array as data of ajax function where i need to get all elements separately. Someone help me get the values ​​in the ajax call. Thanks in advance.

+3


source to share


3 answers


In your PHP file, you can return your array as a json object as follows (notice how the PHP file returns the result to pass it back to javascript)

...
$json = json_encode($row);
echo $json

      

Now add the following to your javascrip ajax

 $.ajax({

...
 // Whatever code you currently have
...

}).done(function ( json ) {

    var data = JSON.parse(json);

    //Continue with javascript
});

      

You can treat the java script variable 'data' as an associative array like it did in PHP



also see how you populate the PHP $ row variable and set up the following way:

$cnt = 0;
while ($r = mysql_fetch_assoc($queryresult)) {

        $row[$cnt] = $r;
        $cnt++;

} 
 $json = json_encode($row);
    echo $json;

      

in javascript you can access your strings like this in a data variable:

var value = data[0]['field_name'];

      

in the above statement, 0 will match the value of $ cnt (i.e. the line number returned by mysql)

0


source


return $row;

      

replace this like

echo json_encode($row);

      



and add dataType = 'json' to ajax like this

dataType: 'json',

      

0


source


If you want to get the value in the ajax call I think it would be:

    while ($r = mysql_fetch_assoc($queryresult)) {
       $row[] = $r;
    }

    echo json_encode(array('data'=>$row));
    exit;

      

0


source







All Articles