Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have to retrieve many rows from MySQL and send by encoding with ajax and I done this. but the problem is I am not able to handle the output array data in ajax. can anyone help me please?

1>one.php

<?php   



  $host = "localhost";
  $user = "root";
  $pass = "";

  $databaseName = "elearning";
  $tableName = "users";



    $con = mysql_connect($host,$user,$pass);
    $dbs = mysql_select_db($databaseName, $con);
    if(isset($_POST)){

                $exam_id=$_POST['exam_id'];
                $sql="select * from exam_to_question where exam_id=$exam_id";
                $result = mysql_query($sql);

                $dataArray = array();

                while($array = mysql_fetch_assoc($result)){
                    $dataArray[] = $array;
                } 

                echo json_encode($dataArray);
    }

?>

2> and ajax code is:

   $.ajax({    
      type: 'POST',
      url: '../functions/one.php',                          
      data: "exam_id="+exam_id,         
      dataType: 'json',
      success: function(data){


             //alert(data[0]['question_id']);
            // i have to handle data here 

            },
      error:function(){
    alert("AJAX failure");
        }   
    });
share|improve this question
2  
Can you post your JSON data? You may look into api.jquery.com/jQuery.each. Also a good read learn.jquery.com/using-jquery-core/iterating –  Satpal Apr 8 at 8:12
    
alert(data); gives me output [object Object],[object Object],[object Object],[object Object],[object Object] . –  Code_Crash Apr 8 at 8:14
    
Learn to use console.log read stackoverflow.com/questions/4539253/what-is-console-log. –  Satpal Apr 8 at 8:14

3 Answers 3

up vote 1 down vote accepted

If that is an array then you have to use .each() method of jQuery:

$.each(data, function(i, resp){
    console.log(resp);
});
share|improve this answer
    
It gives me [object Object] when i alert 'alert(resp);' –  Code_Crash Apr 8 at 8:16
    
@Code_Crash Try to alert resp.exam_id or other fields from row –  Regent Apr 8 at 8:18
    
Thanks sir.I try alert(resp['question_id']); and its done! :) Thanks again! –  Code_Crash Apr 8 at 8:19

You will get jquery object with ajax response. So, you can process it with any of these functions: http://api.jquery.com/each/ http://api.jquery.com/jQuery.each/

share|improve this answer

if you have used dataType: json then you can dirctly use

//if it is not a multidimensional array then you can dirctly
 data.keyName 

//if it is multidimensional array 
$(data).each(function(index,element){
        console.log(element);
})
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.