6

I want to pass the key values into php page.

At php page, I will start to read value by matching ajaxcallid.

But it not working.

It gotta do with syntax/way I am passing in causing error.

parse error
invalid json: ajax call id is missing    

JavaScript/AJAX:

var person = { 
     "Address"    :   "123 Anywhere St.", 
     "City"       :   "Springfield", 
     "PostalCode" :   99999
};

alert(person);          

person= JSON.stringify(person);

alert(person);

$.ajax({
    url: 'ROOT_URL/admin/ajaxtest.php',
    type: "POST",
    dataType: 'json',
    data: {ajaxcallid: '26', jsarr: person},
    timeout: 5000,
    success:  function(output) {
        alert(output.Address);
    },
});

PHP:

<?php
if (isset($_REQUEST['ajaxcallid']))
{    
    if($_REQUEST['ajaxcallid']==26)
    {    
        //example, I want to read value of person.Address, person.City, 
        //person.PostalCode
    //what is the easiest way
        $phparr= json_decode($_REQUEST['jsarr']);
        //do all other operation
        $output= json_encode($phparr);
    }
}
else
{
    $output= "ajax call id is missing";
}
echo $output;
?>
1
  • Please dump your $phparr just before encoding as JSON and paste the output. Commented Feb 14, 2011 at 5:51

6 Answers 6

32

If you are not using dataType : 'json', you might need to do stripslashes

$.post(window.data.baseUrl, {posts : JSON.stringify(posts)});

And in php:

$posts = json_decode(stripslashes($_POST['posts']));
Sign up to request clarification or add additional context in comments.

Comments

7

This helped me:

 data = json_decode($this->request->data['jsarr'], true);

in your php code for accessing the record

Hope it will help someone!

Comments

5

I'm going to take a guess and say that you SHOULDN'T stringify anything. I believe JQuery will do that for you. Namely, no person = JSON.stringify(person). Give that a try.

2 Comments

absolutely agree. why would you encapsulate the stringified parameters in an object and then send them through ajax, when jquery passes an OBJECT of parameters.
max_input_vars in PHP 5.3.9. limits input vars to 1000 if you have huge json file then it can give you PHP warning.
3

This is what your $.ajax call and the PHP side should look like:

JQuery

$.ajax({
    url: "/admin/ajaxtest.php",
    method: "POST",
    data: {
        ajaxcallid: "26",
        person: JSON.stringify({
            "Address" : "123 Anywhere St.",
            "City" : "Springfield",
            "PostalCode" : "99999"
        })
    }
}).done(function(data) {
    if (!data) {
        // generic error message here
    } else if (data == 'invalid') {
        alert('no ajaxcallid received');
    } else {
        var result = $.parseJSON(data); // if you pass back the object
        alert(result.Address);
    }
});

PHP

if (isset($_REQUEST['ajaxcallid'])) {
    if ((int) $_REQUEST['ajaxcallid'] == 26) {
        $personData = json_decode($_REQUEST['person']);
        $address = $personData->Address;
        $postalCode = $personData->PostalCode;
        $returnData = json_encode($personData);
        echo $personData;
        die();
    }
} else {
    echo 'invalid';
    die();
}

Comments

2
$data_array = json_decode($json_string);

If you want objects to be converted into associative arrays, then add true into function:

$data_array = json_decode($json_string, true);

Comments

0

I have not worked with PHP but from my experience with ASP.net following may help you.

Add contentType key to ajax settigns:

type: "POST",
contentType:'application/json',
dataType: 'json',

also I think you need to stringify whole value you are assigning to data like this:

var person = { 
     "Address"    :   "123 Anywhere St.", 
     "City"       :   "Springfield", 
     "PostalCode" :   99999
};

var d= {ajaxcallid: '26', jsarr: person};
var dat=JSON.stringify(d);


......
data: dat,
......

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.