1

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
Community
  • 1
  • 1
john
  • 775
  • 3
  • 15
  • 31
  • 2
    It seems as though you have xdebug ON that is adding extra cruft to your output (along with `error!!!`) – Naftali Jun 28 '16 at 18:28
  • Make sure that in your production code, there's no possibility of someone viewing your SQL or table details through an error. – 4castle Jun 28 '16 at 18:31
  • Once you fix the php errors/warnings, you will want to remove the `echo "error!!!";` and just echo the json. It will be malformed if you don't – Rasclatt Jun 28 '16 at 18:31
  • Thanks, I've removed the `error!!!`, and @4castle, I'm trying to work through error reporting at the moment, I'll remove the table details and replace with a more generic error when I figure out how to get the `data.error` to show – john Jun 28 '16 at 18:59
  • Are you just getting a string of `{"error":{"message":"INSERT INTO Orders (clientID, dateSubmitted , type)\r\nVALUES ('0', '', 'internal')","code":null}}`? If so, yo have to convert the string back `var useData = JSON.parse(data);` – Rasclatt Jun 28 '16 at 19:14

1 Answers1

2

Your PHP script is outputted PHP errors, this causing the JSON to be invalid.

You can either disable errors from showing:

error_reporting(0);

or fix the errors.

Jamie Bicknell
  • 2,306
  • 17
  • 35
  • I've added the `error_reporting(0);` to my php code, and I also removed the error!!! echo. I am still getting `no data error` is there something else i'm missing? – john Jun 28 '16 at 18:58
  • If the SQL is a success then the response isn't JSON. Perhaps look into that. – Jamie Bicknell Jun 28 '16 at 18:59
  • Ah, It was JSON, put it needed to be parsed `var response=jQuery.parseJSON(data);` that does the trick and gets it working. thanks for the advice. – john Jun 28 '16 at 19:31