0

I'm new at AJAX and I am just trying to display the database results from PHP to HTML via AJAX. I'm getting "undefined" on console log when I load the page. HTML, PHP and the JS files are separated from each other.

Here's my JS Code:

$(document).ready(function(){

$.ajax({

    type: "GET",
    url:  "xaja.php",
    dataType: "json",
    contentType: "application/json; charset=utf-8",

    success: function(data){
        console.log(""+data.name);
    },
    error: function(e){
        alert("Error:\n"+e);
    }

});

});

Here's my PHP Code:

<?php

$json = array(

'username' => '',
'name' => '',
'loc' => ''

);


$sql = "Select * from tbluser";
$query = mysql_query($sql, $conn);
$result = mysql_fetch_assoc($query);

do{
    $json['username'] = $result['username'];
    $json['name'] = $result['name'];
    $json['loc'] = $result['location'];
    echo json_encode($json);
    $result = mysql_fetch_assoc($query);

}
while($result);

?>

Am I missing something here? Thanks in advance!

6
  • Use console.log instead of alert to debug Javascript - alert only handles strings or string-like values, console.log can output nearly anything. You'll be able to see what the value of e is (an object) and debugging will become much easier. Commented Apr 21, 2015 at 14:08
  • Thanks Scott, here's the error now: Object { readyState: 4, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 11 more… } Commented Apr 21, 2015 at 14:09
  • It tells me undefined. Commented Apr 21, 2015 at 14:10
  • 1
    You are sending of countless responses due to the while loop. Try gathering the json-arrays of yours in a superset array and do a single response. Commented Apr 21, 2015 at 14:12
  • e.message should work. Or try e.name. Both should be supported by all browsers! Commented Apr 21, 2015 at 14:18

3 Answers 3

3

You are outputting multiple json strings. In order for your respond to be parsed it needs to be a single string. Try this instead:

$results = array();
do{
    $json['username'] = $result['username'];
    $json['name'] = $result['name'];
    $json['loc'] = $result['location'];
    $results[] = $json;
    $result = mysql_fetch_assoc($query);
}
while($result);
echo json_encode($results);
Sign up to request clarification or add additional context in comments.

8 Comments

still getting an object object.
It's now successful, but still getting an undefined data on console.log :(
What does it output when you do console.log(data);?
"[object Object],[object Object]" - and yeah I have 2 data in my database.
Are you adding a "". or something inside the console.log or something? I ask because when you just do a console.log(data) it should actually show you the object data
|
0

why not traditional way?

$sql = "Select * from tbluser";
$query = mysql_query($sql, $conn);

if ($result = mysql_fetch_assoc($query) ) {
    $json['username'] = $result['username'];
    $json['name'] = $result['name'];
    $json['loc'] = $result['location'];
    echo json_encode($json);
}

or if you need array of array:

$json = array();
$sql = "Select * from tbluser";
$query = mysql_query($sql, $conn);

while ($result = mysql_fetch_assoc($query) ) {
    $json[] = array($result['username'], $result['name'], $result['location']);
}
echo json_encode($json);

Comments

0

When returning JSON it should be a single structure, instead of outputting an array for each row, you should rather be building the array and outputting it once.

$json = array();
do {
    $json[]=$result;
    $result = mysql_fetch_assoc($query);
} while($result);
echo json_encode($json);

Which shows each row as a named structure ([{username:"",name:"",location:""},{}..])

Comments

Your Answer

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