0

To update a Javascript Array (eventDates) I want to get the content via Ajax/PHP – sadly I don’t get it to work. Anyone knows how the PHP output (data) hast to look, that I can work with it?

      $.ajax({
        type: "GET",
        url: "_ajax_registration.php?type=getDate&project="+val,
        data: "data",
        success: function(data){
            var eventDates = data;
        }
      });

      /*
      var eventDates = [{date: new Date(2014, 1-1, 28)}},
                {date: new Date(2014, 1-1, 18)},
                {date: new Date(2014, 6-1, 18)},];
      */


      function showEventDates(date) {
        for (var i = 0; i < eventDates.length; i++) {
            if (date.getTime() == eventDates[i].date.getTime()) {
                return [true, ''];
            }
        }
        return [false, ''];
      }

      $("#datepicker").datepicker("destroy");       
      $( "#datepicker" ).datepicker({       
        beforeShowDay: showEventDates

      }); 
      $( "#datepicker" ).datepicker( "refresh" );   
0

3 Answers 3

1

You have an excrescent bracket after the first and an redundant comma on the last object. Try this:

 var eventDates = [{date: new Date(2014, 1-1, 28)},
            {date: new Date(2014, 1-1, 18)},
            {date: new Date(2014, 6-1, 18)}];
Sign up to request clarification or add additional context in comments.

2 Comments

Also need to get rid of that last , for IE to work.
Sorry Peter, I was in the wrong row when answering. Your name and correction are appreciated.
0

You need to echo json_encode($someArray) from _ajax_registration.php.

Also, you won't be able to access eventDates outside of your ajax callback. You'll need to do:

function showEventDates(date) {
  for (var i = 0; i < eventDates.length; i++) {
    if (date.getTime() == eventDates[i].date.getTime()) {
      return [true, ''];
    }
  }
  return [false, ''];
}

$.ajax({
  type: "GET",
  url: "_ajax_registration.php?type=getDate&project="+val,
  success: function(data){
    var eventDates = data;
    $("#datepicker").datepicker("destroy");       
    $( "#datepicker" ).datepicker({       
      beforeShowDay: showEventDates
    }); 
    $( "#datepicker" ).datepicker( "refresh" );
  }
});

6 Comments

thanks for your fast and good awnser, sadly I have no idea how $someArray should look like to get it work ;(
If you comment out your static eventDates do you get the expected result?
That works fine. I just added an alert to see the result, with the comment code i get "[object Object],[object Object]" and with the value from data "["date: 1387926000","date: 1387926000"]" - my PHP is like this: $myArray[] = 'date: '.strtotime('25-12-2013'); echo json_encode($myArray);
You would need to do like $myArray[] = array('date' => strtotime('25-12-2013'));
You don't want to try to build a json looking string, you want to use native data types.
|
0

What I like to to is, on the php side, take your array and

echo json_encode($myArray));

then, in the jquery you canjust:

$.GetJson('ajax_reg.php', { type : 'getDate', project': val }, function(data) {
    eventDates = date;
});

1 Comment

thanks for your fast awnser, sadly I have no idea how $myArray should look like ;( $myArray[] = strtotime('18-01-2014')*1000; doenst work

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.