How to retrieve values from a JSON Object in PHP

I've tried this many times now, using difference methods and none of them have worked for me, so I'm asking this question.

I have a small form that takes in 4 pieces of information, a persons title, first name, middle name and last name. When I hit a button, a JSON Object is formed, and sent as post data through a jQuery.ajax method.

JSON Sent to 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 a Javascript Object that is then stringyfied and posted as a parameter name 'json'.

In my php file I assign the posted string to a variable, name $json, I then decode using json_decode(), do some funky business, and send a response back to the 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 the moment I just want to return the title property of the json object that was sent in the post request, but anything I return comes back as null. If I echo back the string, without decoding it, then I get the following:

{\"title\":\"Mr\",\"firstName\":\"Banana\",\"middleName\":\"Slippy\",\"lastName\":\"McDougle\"}

If I try to extract values using: $title = $json->title; and put that into a MYSQL statement, it's inserted as null or blank, nothing gets input.

Am I doing something wrong? Or have I somehow got an outdated version of PHP to handle JSON? And help is greatly appreciated.

Answers


Why do you want to send JSON to the PHP script? What's wrong with using a 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 will convert obj to a query string, and then you can just do $_POST['firstName'] and $_POST['lastName'].


Need Your Help

How to check if an element is inside an iframe or not

javascript html iframe

Let's say you have a DOM node and you want to know whether it is located inside an iframe or not. One way would be to check it's parent chain to see if you reached an iframe before reaching the par...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.