javascript - How to retrieve values from a JSON Object in PHP -


i've tried many times now, using difference methods , none of them have worked me, i'm asking question.

i have small form takes in 4 pieces of information, persons title, first name, middle name , last name. when hit button, json object formed, , sent post data through jquery.ajax method.

json sent php file:

{     "title": "mr",      "firstname":"banana",     "middlename":"slippy",     "lastname":"mcdougle" } 

ajax call on button press:

    function insertperson(obj, callback){         $.ajax({             type: "post",             data: "json=" + json.stringify(obj),             url: "insertdata.php",             success: function(obj){                 if (callback){ callback(obj) };             },             error: function(error){                 console.log("error:");                 console.log(error);             }         });     } 

i pass in javascript object stringyfied , posted parameter name 'json'.

in php file assign posted string variable, name $json, decode using json_decode(), funky business, , send response browser.

insertdata.php:

require ('connect.php'); header('content-type: application/json');  $json_string = $_post['json']; $json = json_decode($json_string); ...do server related inserts/selects etc... echo json_encode($json->title); 

at moment want return title property of json object sent in post request, return comes null. if echo string, without decoding it, following:

{\"title\":\"mr\",\"firstname\":\"banana\",\"middlename\":\"slippy\",\"lastname\":\"mcdougle\"} 

if try extract values using: $title = $json->title; , put mysql statement, it's inserted null or blank, nothing gets input.

am doing wrong? or have somehow got outdated version of php handle json? , appreciated.

why want send json php script? what's wrong using query string?

$.ajax({     type: "post",     data: obj,     url: "insertdata.php",     success: function(obj){         if (callback){ callback(obj) };     },     error: function(error){         console.log("error:");         console.log(error);     } }); 

this convert obj query string, , can $_post['firstname'] , $_post['lastname'].


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -