i have following code snippet below. when "create order" button clicked, user inputted data (not shown in code) converted json, via function "converttojson". able convert data json. problem passing json object php can insert object database. php returning "null" value when run $var_dump.
note trying post same page, "orders.php"
<head> <script type="text/javascript"> function converttojson() { var tabledata = $('#productorder').tabletojson({ ignorecolumns: [0] }); var xhr = new xmlhttprequest(); xhr.open("get","orders.php",true); xhr.setrequestheader("content-type", "application/json"); xhr.send(json.stringify(tabledata)); } </script> </head> ...more code... <button type="button" class="btn btn-success btn-md" style="float: right;" onclick="converttojson();">create order</button> <div> <?php $data = json_decode(file_get_contents('php://input')); var_dump($data); ?> </div>
you should use actual ajax request post data
$.ajax({ url: 'yourscript', type: 'post', data: { data: tabledata } datatype: "json", beforesend: function() { //do } }).done(function(msg){ //do when done }).fail(function(msg){ //error handling }).complete(function(msg){ //do when completed }); }
and data $_post["data"]
also note stated before need take care of script lifecycle working async requests
Comments
Post a Comment