1

Can some one tell why this simple JSON with JqUERY doesn't work for me?

I have this JS code,

var jsonParam = <? $json = $_SESSION['searchSess']; echo json_encode($json);?>;
jsonParam = JSON.stringify(jsonParam);
$(document).ready(function(){
      $.post("searching.php?rdr=search", {data: jsonParam,}, function (data){
        alert(data)
    })
});

And Here is the PHP code,

$data = json_decode($_POST['jsonParam'], true);
var_dump($data);

And the response is null or nothing,

can please someone help whats wrong here?

Thank you

5 Answers 5

4

You want $_POST['data'], not $_POST['jsonParam'].

Sign up to request clarification or add additional context in comments.

Comments

3

jsonParam was the JavaScript variable, but it was posted to PHP as $_POST['data'] since you passed {data: jsonParam} into the $.post.

// Instead:
$data = json_decode($_POST['data'], TRUE);
var_dump($data);

6 Comments

Thank you, but this also returns null, :(
var_dump($_POST) to see if you got anything, and also look at your page source to be sure that the original json_encode($json) printed what it was supposed to.
Ok, with var_dump($_POST) I'm getting this [data] => {\"locations\":\"ARP\"}, so how do I perse the array?
@thegrede json_decode($_POST['data'], TRUE) handles that just fine for me, $data["locations"] is ARP
But when I do json_decode($_POST['data'], TRUE) I'm getting nothing, :(
|
2

{jsonParam: jsonParam,} Instead of {data: jsonParam,}

Comments

1

Try the following:

JS:

var jsonParam = <? 
    $json = $_SESSION['searchSess']; 
    $json['longitude'] = (string) $json['longitude'];
    $json['latitude'] = (string) $json['latitude'];

    echo json_encode($json);
?>

$(document).ready(function(){
      $.post("searching.php?rdr=search", {data: jsonParam }, function (data){
        alert(data)
    })
});
 

PHP:

$data = json_decode($_POST['data'], true);
var_dump($data);

I suspect your longitude and latitude fields are not being parsed correctly as floats.

8 Comments

Nops!! :( it retruns empty too, I dont know why, i'm copying it and paste too and no string returns, brrrrr.
Updated, try again, I missed the quotes on JSON.parse.
Ye, I tried that with the quotes before too, but the same :( no stritng returns, I guess its somethings else where, and need to be figure out, does it works when psste it in your server?
I don't know what your $_SESSION['searchSess'] is so it's hard for me to tell if it would work :) can you post what you get from doing echo json_encode($json); on a blank page?
This is the result from a blank page, {"locations":"ARP","pickupair":"JFK","dropoffair":"JFK","pickUpLocation":"John F. Kennedy International Airport (JFK), 144-02 135th Ave, Queens, NY 11436, USA","dropOffLocation":"","pickupmonth":"08","drol":"false","pickupday":"23","pickupyear":"2012","dropoffmonth":"08","dropoffday":"30","dropoffyear":"2012","pickupmin":"09","pickuphour":"00","dropoffpmin":"09","dropoffhour":"00","latitude":4064384.36,"longitude":-7378230.35,"relatitude":0,"relongitude":0,"hourPU":"18","hourDO":"18"}
|
1

$data = json_decode($_POST['jsonParam'], true); should be $data = json_decode($_POST['data'], true);

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.