javascript - Unable to get JSON from Ajax POST using PHP -
i sending json in post php script. i'm unable object values json. format of post follows:
[{"name":"amount","value":"12"}]
javascript
("#idform").submit(function(e) { var url = "post.php"; // script handle form input. var formdata = json.stringify($("#idform").serializearray()); alert(formdata); $.ajax({ type: "post", url: url, data: formdata, datatype: 'json', success: function(dataresponse) { document.getelementbyid("orderamount").innerhtml = dataresponse.orderamount; } });
php
if(!empty($_server['http_x_requested_with']) && strtolower($_server['http_x_requested_with']) == 'xmlhttprequest') { $data = array(); $json = json_decode(file_get_contents('php://input'), true); $data['orderamount'] = $json['amount']; //this works //$data['orderamount'] = '12345'; echo json_encode($data); }
any idea i'm doing wrong?
we can parse json array, , dictionaries inside it, this,
<?php $json = "[{\"name\":\"amount\",\"value\":\"12\"}]"; $dec = json_decode($json,true); for($idx = 0; $idx < count($dec); $idx++){ $obj = (array)$dec[$idx]; echo $obj["name"]; } ?>
Comments
Post a Comment