1

I have the following function:

function myFunction () {
    $.getJSON('remote.php', function(json) {
        var messages = json;

        function check() {
             ...        

and I call there the remote.php script which makes a simple select query and returns all data with json. I would like to pass a parameter to this query called time, which will be filled earlier in my code by:

var actualTime = new Date(params);

I know that on php script I have to do:

$time = $_POST['time'];

but how should I modify my jquery then to pass this argument further?

user3766930
  • 5,629
  • 10
  • 51
  • 104

1 Answers1

1

Just pass an object to $.getJSON. It will be sent to PHP as $_GET.

$.getJSON('remote.php', {
    time: actualTime.toJSON()
}, function(json) {
    var messages = json;
});

Then your date will be in PHP as $_GET['time']. I'd suggest converting to a DateTime object so you can format it as you need.

$time = new DateTime($_GET['time']);

If you want to use $_POST instead, then you'll have to change to using $.post.

$.post('remote.php', {
    time: actualTime.toJSON()
}, function(json) {
    var messages = json;
}, 'json');
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • thanks, why did you use actualTime.toJSON()? Can I just write: `time: actualTime` ? – user3766930 Oct 09 '15 at 20:21
  • 1
    @user3766930: `actualTime` is a `Date` object. I used `toJSON()` to convert it to a string that PHP understands. – gen_Eric Oct 09 '15 at 20:26
  • Thank you very much! Is there a way of passing it with POST instead of GET? and is there any specific reason why you suggested GET instead of POST? – user3766930 Oct 09 '15 at 20:42
  • @user3766930: If you are using the `$.getJSON` function, then the parameters will be sent via `GET`. If you need to use `POST`, you'll have to switch to `$.post` instead. – gen_Eric Oct 09 '15 at 20:44