• 4
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Passing data back from php to ajax

How can I pass data from a php of then rows back to ajax ?

PHP

$query = 'SELECT * FROM picture order by rand() LIMIT 10';
$result = mysql_query($query);

while ($rec = mysql_fetch_array($result,  MYSQL_ASSOC)) {

$url[]=$rec['pic_location'];
$name[]=$rec['name'];
$age[]=$rec['age'];
$gender[]=$rec['gender'];


}

echo json_encode($url);
echo json_encode($name);
echo json_encode($age);
echo json_encode($gender);

Ajax

$(".goButton").click(function() {
   var dir =  $(this).attr("id");
   var imId = $(".theImage").attr("id");

   $.ajax({
      url: "viewnew.php",
      dataType: "json",
      data: {
         current_image: imId,
         direction    : dir
      },
      success: function(ret){
          console.log(ret);
          var arr = ret;
          alert("first image url: " + arr[0][0] + ", second image url: " + arr[0][1]);  // This code isnt working
          alert("first image Name: " + arr[1][0] + ", second image name: " + arr[1][1]);
          $(".theImage").attr("src", arr[0]);
          if ('prev' == dir) {
            imId ++;
         } else {
            imId --;
         }
         $("#theImage").attr("id", imId);
      }
   });

});
});
</script>

My question is how can I display the values here ? The Alert message is giving me "Undefined" ?

You can do something along these lines.

PHP

$query = 'SELECT * FROM picture order by rand() LIMIT 10';
$res = mysql_query($query);

$pictures = array();
while ($row = mysql_fetch_array($res)) {
  $picture = array(
    "pic_location" => $row['pic_location'],
    "name"         => $row['name'],
    "age"          => $row['age'],
    "gender"       => $row['gender']
  );
  $pictures[] = $picture;
}

echo json_encode($pictures);

JS

...
$.ajax({
  ...
  dataType: "json",
  ...
  success: function(pictures){
    $.each(pictures, function(idx, picture){
      // picture.pic_location
      // picture.name
      // picture.age
      // picture.gender
    });
  }
});
...
  • 12
Reply Report
    • Thankyou :) , Would the echo json_encode($picture); be returning just the 1st row or all the 10 rows from the database ? I just tried it , its echoing just the 1's row ?
    • One more question , We get our response and that object is stored in variable pictures. Why doesn't pictures(3).name give me the name of the 3rd record ? How could I access that ?

You can't put multiple echo statements for the AJAX response:

echo json_encode($url);
echo json_encode($name);
echo json_encode($age);
echo json_encode($gender);

Join your arrays and send a single response:

$arr = $url + $name + $age + $gender;
echo json_encode($arr);
  • 2
Reply Report

You can easily do this using a single Array:

$pics = array();

while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $pics[$rec['id']]['url'] = $rec['pic_location'];
  $pics[$rec['id']]['name']=$rec['name'];
  $pics[$rec['id']]['age']=$rec['age'];
  $pics[$rec['id']]['gender']=$rec['gender'];
}

echo json_encode($pics);
  • 2
Reply Report
    • @srinivasan - Thanks , But i wanted to send the entire 10 rows of a database back ? I think this would just send the 1's row ? Isnt it ?
      • 2
    • @Alexander and Adi Mathur, I've corrected the errors now. Please see the updated code.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions