0

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.

Andrew Burns
  • 63
  • 1
  • 8
  • What version of php do you have? older versions of it didn't have json_decode or encode, but if that were the case, you'd be getting php errors. – Kevin B May 02 '13 at 18:26
  • Do you have magic quotes enabled? Have you tried.. $json_string = stripslashes($_POST['json']); before decoding the string. – Keith Lantz May 02 '13 at 19:02

2 Answers2

1

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'].

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • I want to try it with JSON first as I'll be implementing this into a previous project that also uses JSON as it's main data format. I want to keep things consistent, if it comes to it, then I suppose I'll have to use another format. – Andrew Burns May 02 '13 at 21:19
  • I'm going to use this method, as it turns out, I can't send JSON no matter what method I try, either I'm doing it wrong or I've got some sort of encoding issue that I'm not quite aware of. I'll stick to sending a query string, and send the responses back to the browser in JSON format. – Andrew Burns May 03 '13 at 07:23
  • @AndrewBurns: I usually like this method as PHP will "parse" it for you. Also, I think the JSON might be urlencoded since it was sent via the GET string. – gen_Eric May 03 '13 at 13:37
0

I think this is the best shot for you.

jQuery Ajax POST example with PHP

Community
  • 1
  • 1
sven
  • 775
  • 5
  • 14