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");
        }   
    });
3

3 Answers 3

1

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

$.each(data, function(i, resp){
    console.log(resp);
});
Sign up to request clarification or add additional context in comments.

3 Comments

It gives me [object Object] when i alert 'alert(resp);'
@Code_Crash Try to alert resp.exam_id or other fields from row
Thanks sir.I try alert(resp['question_id']); and its done! :) Thanks again!
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/

Comments

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);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.