My php returns something as such:
if(!$result = $db->query($sql)){
echo $db->error;
die('There was an error running the query [' . $db->error . ']');
}
echo 'Total results: ' . $result->num_rows . "\n";
while($row = $result->fetch_assoc()){
echo json_encode($row);
echo "\n";
}
In my javascript, I want to put the output on the page if it's a json object or just log it to the console if it's something else:
var divOutput = function(output) {
try {
// var x = output.trim();
// x = JSON.parse(JSON.stringify(x));
var x = JSON.parse(output);
console.log(x);
$("#dbOut").html(JSON.stringify(x, null, '\t'));
} catch (e) {
console.log(output);
console.log(e);
}
}
var getPlayerByID = function() {
var myNumber = document.getElementById("PlayerInput").value;
$.ajax({
url : "db_funcs.php",
data : {
action : 'getPlayerByID',
a : myNumber,
},
type : 'post',
success : function(output) {
divOutput(output);
}
However it's throwing the JSON parse error when I query the database. How can I do this? }) }