I'm trying to figure out how to return error's from my php script to my jquery function. I've gone with the top answer from this question where I echo a json object that I return inside the data object but I cannot see the individual parts of the json object. How do I access the data json objects from the return of the post? my console always shows no data error.
$.post("../dist/scripts/submitOrder.php",
{
clientID: clientID,
date: date,
po: po,
orderType: orderType,
orderlines:JSON.stringify(productArray)
},
function(data, status){
console.log("Data: " + data + "\nStatus: " + status);
if(data.error){
console.log("BIG ERROR:" + data.error.message);
} else {
console.log("no data error");
}
})
.fail(function() {
console.log("Fail Data: " + data + "\nStatus: " + status);
});
relavent .php script
if ($conn->query($sql) === TRUE) {
echo "New record created successfully ";
} else {
$conn->close();
//echo "Error: " . $sql . "<br>" . $conn->error;
echo "error!!!";
echo json_encode(array('error' => array(
'message' => $sql,
'code' => $conn->error,
)));
exit;
}
my console shows this
Data: <br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: clientID in C:\wamp\www\admin\startbootstrap-sb-admin-2-1.0.8\dist\scripts\submitOrder.php on line <i>11</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0015</td><td bgcolor='#eeeeec' align='right'>259296</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\admin\startbootstrap-sb-admin-2-1.0.8\dist\scripts\submitOrder.php' bgcolor='#eeeeec'>..\submitOrder.php<b>:</b>0</td></tr>
</table></font>
error!!!<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: main(): Couldn't fetch mysqli in C:\wamp\www\admin\startbootstrap-sb-admin-2-1.0.8\dist\scripts\submitOrder.php on line <i>32</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0015</td><td bgcolor='#eeeeec' align='right'>259296</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\admin\startbootstrap-sb-admin-2-1.0.8\dist\scripts\submitOrder.php' bgcolor='#eeeeec'>..\submitOrder.php<b>:</b>0</td></tr>
</table></font>
{"error":{"message":"INSERT INTO Orders (clientID, dateSubmitted , type)\r\nVALUES ('0', '', 'internal')","code":null}}
Status: success
VM256:279 no data error