0

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");
        }   
    });
Code_Crash
  • 734
  • 5
  • 22
  • 2
    Can you post your JSON data? You may look into https://api.jquery.com/jQuery.each/. Also a good read https://learn.jquery.com/using-jquery-core/iterating/ – Satpal Apr 08 '14 at 08:12
  • alert(data); gives me output [object Object],[object Object],[object Object],[object Object],[object Object] . – Code_Crash Apr 08 '14 at 08:14
  • Learn to use `console.log` read http://stackoverflow.com/questions/4539253/what-is-console-log. – Satpal Apr 08 '14 at 08:14

3 Answers3

1

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

$.each(data, function(i, resp){
    console.log(resp);
});
Jai
  • 74,255
  • 12
  • 74
  • 103
0

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/

0

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);
})
Jaimin MosLake
  • 655
  • 1
  • 12
  • 30