javascript - Ajax - return result of PHP script to PHP -
probably stupid question - have form allows user submit 1 number. using number, use 3rd party api process request , in return receive array of arrays.
i trying request via ajax - submit request via ajax, , have return working. however, don't want parse 2d array returned in javascript - how parse in php?
index.php
<html> <head> <meta charset="utf-8"> <title></title> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script type="text/javascript"> $("document").ready(function(){ $(".nd-tracking").submit(function(){ var data = { "action": "test" }; data = $(this).serialize() + "&" + $.param(data); $.ajax({ type: "post", datatype: "json", url: "nd-request.php", //relative or absolute path response.php file data: data, success: function(data) { $(".the-return").html( "<pre>" + data["json"] + "</pre>" // $.each(data, function(key,value) { // console.log(value.code[0] + ' ' + value.date[0] + ' ' + value.time[0]) // }) ); } }); return false; }); }); </script> </head> <body> <form action="nd-request.php" method="post" class="nd-tracking"> consignment id: <input type="text" value="" placeholder="consignment id" name="consignmentid"><br> <input type="submit" name="submit" id="submit"> </form> <div class="the-return"> </div> </body> </html>
nd-postrequest
<?php if (is_ajax()) { if (isset($_post["action"]) && !empty($_post["action"])) { //checks if action value exists $action = $_post["action"]; switch($action) { //switch case value of action case "test": test_function(); break; } } } //function check if request ajax request function is_ajax() { return isset($_server['http_x_requested_with']) && strtolower($_server['http_x_requested_with']) == 'xmlhttprequest'; } /* * sending tracking request via xml netdespatch * input - url request being submitted * xml request * returns xml response netdespatch */ function sendtrackingrequesttonetdespatch($url, $xml_data) { $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_httpheader, array('content-type: application/x-www-form-urlencoded')); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_postfields, $xml_data); $result = curl_exec($ch); curl_close($ch); return $result; } /* * count number of nodes in tracking node (each node stage of delivery proces) * input - parsed xml simplexml_load_string * returns integer number of stages consignment has been through */ function numberofstages ($xml){ foreach ($xml->response->gettrackresponse $stage) { $num_of_stages = $stage->count(); } return $num_of_stages; } function test_function(){ $return = $_post; $consignmentid = $_post["consignmentid"]; $xml_data = 'ndxml=<ndxml>' . 'blahblah' . '</ndxml>'; $url = "urlofrequest"; // submit xml request , grab response $output = sendtrackingrequesttonetdespatch($url, $xml_data); // parse xml response $xml=simplexml_load_string($output) or die("error: cannot create object"); // check 'status' xml response - if ok, proceed $status = $xml->response->status; if ($status == "ok - data follow") { // count number of stages consignment has passed $num_of_stages = numberofstages($xml); // define empty array filled using while loop each stage $stages = array(); while ($num_of_stages > 0) { $num_of_stages--; // decrementing first xml array 0 based // buidling 2d array each stage , sub-info $stages[$num_of_stages]['code'] = $xml->response->gettrackresponse->tracking[$num_of_stages]['code']; $stages[$num_of_stages]['date'] = $xml->response->gettrackresponse->tracking[$num_of_stages]->trackdatetime->date; $stages[$num_of_stages]['time'] = $xml->response->gettrackresponse->tracking[$num_of_stages]->trackdatetime->time; if (isset($xml->response->gettrackresponse->tracking[$num_of_stages]->{'track-event'})){ $stages[$num_of_stages]['trackevent'] = $xml->response->gettrackresponse->tracking[$num_of_stages]->{'track-event'}; } if (isset($xml->response->gettrackresponse->tracking[$num_of_stages]->{'carrier-event'})){ $stages[$num_of_stages]['carrierevent'] = (string)$xml->response->gettrackresponse->tracking[$num_of_stages]->{'carrier-event'}; } } // reverse results array can display results starting first event $return = array_reverse($stages); } elseif ($status == "no order found given consignment number") { $return .= "no order found given consignment number."; } $return["json"] = json_encode($return); echo json_encode($return); } ?>
the response
[{"code":{"0":"bkd"},"date":{"0":"2016-06-30"},"time":{"0":"08:51:33"},"trackevent":{"0":"magda sliwa"}},{"code":{"0":"acc"},"date":{"0":"2016-06-30"},"time":{"0":"11:59:43"},"trackevent":{"0":"depot"},"carrierevent":"accepted"},{"code":{"0":"crd"},"date":{"0":"2016-07-01"},"time":{"0":"09:56:00"},"carrierevent":"closed \/ carded"}]
i want take response , process via php.
Comments
Post a Comment