Ok, I spend many time on this, I don’t get what I’m doing wrong.
It seems impossible to get the data in the PHP file.
- First I call many times “copy” to fill the “result” array.
- Then, I call the
$.ajaxmethod - In the process.php $_POST is empty
–> In the PHP $x, $y or $time or not null but not empty.
Edit 2:
Ok – with json_last_error() i saw that it’s my json which is
“Syntax error: malformed”. But i don’t know how to encode it better than what i’m
doing.So i cheat by adding a stripslashes() on the $_POST.
[{\”x\”:104,\”y\”:218,\”timestamp\”:1349476537434},{\”x\”:90,\”y\”:202,\”timestamp\”:1349476537469},{\”x\”:82,\”y\”:192,\”timestamp\”:1349476537487},{\”x\”:71,\”y\”:177,\”timestamp\”:1349476537514},{\”x\”:68,\”y\”:174,\”timestamp\”:1349476537568},{\”x\”:68,\”y\”:173,\”timestamp\”:1349476537801},{\”x\”:68,\”y\”:174,\”timestamp\”:1349476538478},{\”x\”:68,\”y\”:175,\”timestamp\”:1349476538512},{\”x\”:68,\”y\”:175,\”timestamp\”:1349476538579},{\”x\”:69,\”y\”:175,\”timestamp\”:1349476538678}]
Edit 1:
The posted data seems to be good (look under), and i finish in the
“success function”.[{“x”:529,”y”:97,”time”:1349469608703},{“x”:385,”y”:331,”time”:1349469608720},…..]
JS Side – index.php :
<script src="jquery.js"></script>
results = new Array();
function copy(x, y, time) {
var o = { 'x': x, 'y': y, 'time': time };
results.push(o);
}
function save() {
var encoded_results = JSON.stringify(results);
$.ajax({
url: "process.php",
type: 'POST',
data: {
"results" : encoded_results
},
success: function(data, status, xhr) {
alert(data);
console.log(data);
console.log(xhr);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
PHP Side – process.php :
if(isset($_POST["results"]))
{
$result_json = $_POST["results"];
$JSONArray = json_decode($result_json, true);
if($JSONArray !== null)
{
$x = $JSONArray["x"];
$y = $JSONArray["y"];
$time = $JSONArray["time"]
}
}
On the JavaScript side you are posting an array of objects – a list of x / y / time combinations. The PHP side however ignores the list and object part:
vs.
So overall in a loop maybe: