I have the following code in my script file:
$.ajax({
url: "server.php?saveEvent",
data: "myEvent=" + JSON.stringify(myEvent),
dataType: "json",
type: "post",
success: function (data) {
if (data) {
$("#log").append("<br />Evenement saved.");
}
}
});
In server.php I retrieve the variable using:
if (isset($_GET['saveEvent'])) {
if (isset($_POST['myEvent'])) {
$firephp->log(gettype($_POST['myEvent']));
$myEvent = json_decode($_POST['myEvent'], true);
}
}
When I tested this on my localhost, everything went fine. Unfortunately, after deployment,
$myEvent
was empty.
Using firephp, I tested what was in the variable, and I looked at the headers being sent. The object was sent to the server, but still somehow php see’s it as being an empty variable.
Any ideas on how this is possible? Is it a php version or json issue?
EDIT: PHP ver= 5.2.17 / json enabled
EDIT2: Changing te url to ?saveEvent=1 didnt change anything
Edit3: I realize making a get and post is a bit strange, I’ll try changing that, but get/post shouldn’t be a problem I think
Your uri contains a GET parameter,
saveEvent. You’re checking forPOSTdata onlytry checking this:
$_GET['saveEvent']Even so, that parameter hasn’t got a value assigned to it, perhaps change the url to
?saveEvent=1.There’s also no reason for you to use 2 if statements:
Since it seems there’s more going on than just the
GETvs.POSTissue, you might want to add an ampersand (&) at the end of your url: look at your console, the XHR request just pastes the POST parameters at the end of your url, so the uri will look like either one of the following urls:Whereas, what you need is:
Basically, stick to 1 method, either POST or GET, and make sure that the various parameters are separated as they’re supposed to be separated.
I’m not sure if this is the issue, but try
var_dump-ing the$_REQUESTsuper-global, along with$_GET,$_POSTand what have you…