user3383911 user3383911 - 6 months ago 14
JSON Question

Cannot access or print Ajax response in jquery

I am getting a response from my ajax call but how can I access or at least print the response?

Here's my ajax call

$.ajax({
type: "POST",
url: '../backorderReport.php',
data: { from : from, to : to },
dataType: "JSON",
success: function(data){
console.log(data[0].orderID); //not printing anything
console.log(data); //not printing anything
}
});


Here is my php file

$from = $_POST["from"];
$to = $_POST["to"];
$orders = $wpdb->get_results("SELECT * FROM wp_orderrecords WHERE orderDate BETWEEN '".$from."' AND '".$to."'");

foreach($orders as $order){
$orderID = $order->orderID;
$orderDate = $order->orderDate;
$status = $order->status;
$clientID = $order->clientID;
$bill = $order->bill;
$ship = $order->ship;
$pay = $order->pay;
$total = $order->total;
$results = $wpdb->get_results("SELECT * FROM wp_clients WHERE clientID = '".$clientID."%'");

foreach($results as $order){
$clientsName = $order->clientsName;
}

$c = str_replace(' ', " ", $clientsName);

$orderItem = array(
'orderID' => $orderID,
'orderDate' => $orderDate,
'orderStatus' => $status,
'clientsName' => $c,
'bill' => $bill,
'ship' => $ship,
'pay' => $pay,
'total' => $total
);

echo json_encode($orderItem);
}


And I'm getting this response

{"orderID":"26","orderDate":"2016-05-11","orderStatus":"Active","clientsName":"Pebbe\u00a0Kristel\u00a0A
\u00a0Bunoan","bill":"Billed","ship":"Delivered","pay":"Unpaid","total":"1200.00"}{"orderID":"27","orderDate"
:"2016-05-13","orderStatus":"Completed","clientsName":"Lovely\u00a0Carbon","bill":"Billed","ship":"Delivered"
,"pay":"Paid","total":"4650.00"}


How can I print the response and place the data in a table? Thank you for your help!

Answer

You are echoing one valid json string, for each record that matched the query. Hence getting something like :

{ record_1 } { record_2 }

which is invalid json. You want an array like :

[{ record_1 },{ record_2 }]

Modify slightly your php as follows :

 $from = $_POST["from"];
 $to = $_POST["to"];
 $orders = $wpdb->get_results("SELECT * FROM wp_orderrecords WHERE orderDate BETWEEN '".$from."' AND '".$to."'");

$records = [];      // adds an array for the records

foreach($orders as $order){
        $orderID = $order->orderID;
        $orderDate = $order->orderDate;
        $status = $order->status;
        $clientID = $order->clientID;
        $bill = $order->bill;
        $ship = $order->ship;
        $pay = $order->pay;
        $total = $order->total;
    $results = $wpdb->get_results("SELECT * FROM wp_clients WHERE clientID = '".$clientID."%'");     

       foreach($results as $order){
              $clientsName = $order->clientsName;
       } 

       $c = str_replace(' ', " ", $clientsName);

       $orderItem = array(
                'orderID' => $orderID,
                'orderDate' => $orderDate,
                'orderStatus' => $status,
                'clientsName' => $c,
                'bill' => $bill,
                'ship' => $ship,
                'pay' => $pay,
                'total' => $total
             );
       $records[] = $orderItem;  // push each order item on the array
 }
 echo json_encode($records);    // echo the array

ps : test for boundary conditions before the echo, eg. no records found matching the query, and other oddities that could lurk in your server-side software.

Comments