Hi there,
Well, let’s start with ajax.php. What you posted above is JavaScript, let’s convert it to PHP:
$d1 = new DateTime('March 17, 2013 09:22:06');
$d2 = new DateTime('March 20, 2013 22:52:43');
$d3 = new DateTime('April 09, 2013 12:57:51');
$d4 = new DateTime('April 17, 2013 18:03:27');
$d5 = new DateTime('April 18, 2013 00:04:55');
$d6 = new DateTime('April 22, 2013 21:29:20');
Then, one way to do what you want is to create an array containing all of the DateTime objects you have just created, encode it as JSON and send it back to your script:
$arr = array(1 => $d2, 2 => $d6, 4 => $d5, 5 => $d3, 7 => $d4, 15 => $d1);
echo json_encode($arr);
Then in script.js, change the data type accordingly:
dataType: 'json'
And in your addition()
function you can access the JSON thus:
function addition(data){
console.log(data);
}
outputs:
Object {1: Object, 2: Object, 4: Object, 5: Object, 7: Object, 15: Object}
You can access individual members of the array:
function addition(data){
console.log(data[2]);
}
outputs:
Object {date: "2013-04-22 21:29:20", timezone_type: 3, timezone: "Europe/Berlin"}
And you can access individual properties of a member:
function addition(data){
console.log(data[2].date);
}
outputs:
2013-04-22 21:29:20
I hope that helps.
Let me know if you have any more questions.
Just to be complete, here’s the code:
ajax.php
<?php
$d1 = new DateTime('March 17, 2013 09:22:06');
$d2 = new DateTime('March 20, 2013 22:52:43');
$d3 = new DateTime('April 09, 2013 12:57:51');
$d4 = new DateTime('April 17, 2013 18:03:27');
$d5 = new DateTime('April 18, 2013 00:04:55');
$d6 = new DateTime('April 22, 2013 21:29:20');
$arr = array(1 => $d2, 2 => $d6, 4 => $d5, 5 => $d3, 7 => $d4, 15 => $d1);
echo json_encode($arr);
?>
script.js
$.ajax({
url : 'ajax.php',
dataType: 'json',
type: 'post',
success: function(data){
addition(data);
}
});
index.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Unbenanntes Dokument</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="script.js"></script>
<script>
function addition(data){
console.log(data);
}
</script>
</head>
<body>
<h1>Hi</h1>
</body>
</html>