user1725155
user1725155

Reputation: 345

jQuery posting JSON and decoding by PHP

I've been searching all night in stackOverflow and Google to find a way to make my ajax work. I would like to post my data using json to a php form then decoding the json in php and processing them. After processing in php return back some data in json.

Below it my jquery codes which I'm using to send json to php .

        JSONobj = {
            firstname : "david", 
            email : "[email protected]"
        };

        var JSONstr = JSON.stringify(JSONobj);

            $.ajax({
                type: "POST",
                url: "Process.php",

                data: {info: JSON.stringify(JSONobj)},
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data){
                          alert(data.newName);
                        }
            });

but after parsing the json, my header is looks like this :

enter image description here

And what should I put in my PHP file ?

<?php    
$myJson =json_decode($_POST['info'], true);


    // modify the name and return the data in json back
?>

Thanks

Upvotes: 1

Views: 375

Answers (2)

user2027179
user2027179

Reputation: 1

here's what I had to do:

$list = stripcslashes(utf8_encode(urldecode($_POST["list"])));
$obj = json_decode($list);

Then I saw the object when I did var_dump($obj);

Upvotes: 0

StaticVariable
StaticVariable

Reputation: 5283

The problem is that JSON.stringfy() method is use to convert array into json

so use JSONobj=new array();

 data: {info: JSONstr},
or 
 data:JSONstr;

in your php file use

$myjson->firstname;

 // than create a array using 

$newarray=array("firstname"=>$firstname,"lastname"=>$lastname);


echo json_encode($newarray)

Upvotes: 1

Related Questions