0
      $.ajax({
                type: "GET",
                url: 'http://localhost/abc/all-data.php',
                data: {
                    data1: "1"},
                    success: function(response)
                           {
                        alert(response);
                        }          
            });
            return false; 

I want to display each element of array one by one in success function of the ajax currently i get all elements to gether

this is my php code

$i=0;
while($row = mysqli_fetch_assoc( $qry )){

$temp[$i]['c_n'] = $row['c_name'];
$temp[$i]['j_t'] = $row['Job_Title'];
$temp[$i]['des'] = $row['description'];
$temp[$i]['req'] = $row['requirments'];
$temp[$i]['dat'] = $row['posted'];

$i++;
}
$data = array('temp'=> $temp);
echo JSON_encode($temp);

I do appreciate your helps

3 Answers 3

2

you probably use something like this in your success function :

   response.temp.forEach(function(element){
         console.log(element.c_n) ;
         console.log(element.j_t) ;
         console.log(element.des) ;
         console.log(element.req) ; 
         console.log(element.dat) ;
    });
Sign up to request clarification or add additional context in comments.

Comments

1

In your success function, you need to json parse your response

var data = JSON.parse(response);

You can access to your data: data['temp']

If you want your response parsed to json automaticallym you can setup your ajax settings like this:

$.ajaxSetup ({
   contentType: "application/json",
   dataType: 'json'
});

Then you don't need to call JSON.parse anymore.

6 Comments

when i use JSON.parse and alert(data['temp']); it display undefined
@Ahmad please actually show us the code you've used. Simply saying something doesn't work, or shows undefined is useless as it's possible you're not using the method correctly
What does console.log(data) print ?
success: function(response) { var data = JSON.parse(response); alert(data['temp']); //console.log(data); }
@Ahmad Okay and what about console.log(response) ?
|
0

Your code:

$i=0;  while($row = mysqli_fetch_assoc($qry)){
  $temp[$i]['c_n'] = $row['c_name'];
  $temp[$i]['j_t'] = $row['Job_Title'];
  $temp[$i]['des'] = $row['description'];
  $temp[$i]['req'] = $row['requirments'];
  $temp[$i]['dat'] = $row['posted'];
  $i++;
}      $data = array('temp'=> $temp);   echo JSON_encode($temp);

Please change the last line as return JSON_encode($data);

Hope this helps you :)

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.