0

My jquery datepicker has the following code:

$(document).ready(function() { 

    var event = ['14 april 2017', '25 april 2017', '1 april 2017'];

$('#datepicker').datepicker({
      inline: true,
      showOtherMonths: true,
      dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
      beforeShowDay: highlightDays,
      onSelect: function(dateText, inst){
        showpopup();
        getdatespan();
            }
    });


function highlightDays(date) {
      for (var i = 0; i < event.length; i++) {
            if (new Date(event[i]).toString() == date.toString()) {
                return [true, 'ui-state-event', ''];
            }
        }
        return [true, ''];
    }
...

The bit I'd like to focus on is the variable 'event' that is being used to provide dates for the highlightDays function. As it is above, the code works perfectly and successfully highlights the date in the variable. However, those dates have been manually typed in by myself.

Below is the code within my calendar.php file:

$findEventStartdates = $pdo->prepare('SELECT startdate FROM calendar WHERE username=?'); 

//execute query with variables
$findEventStartdates->execute([$username]);


($eventStartdates = $findEventStartdates->fetchAll(PDO::FETCH_COLUMN));

When printed, this shows the following output:

Array ( [0] => 14 april 2017 [1] => 25 april 2017 [2] => 1 april 2017)

My question is how can i get whatever is outputted in that PDO to be used as the variable for the datepicker?

In the PHP file i tried the following:

$highlightEvents = implode("', '",$eventStartdates);

echo <<<_END
 <input type="text" name="eventHighlight" id="eventHighlight" value="'$highlightEvents'">
_END; 

This shows:

'14 april 2017', '25 april 2017', '1 april 2017'

in the textbox and then tried this with the jquery:

var event = [$("#eventHighlight").text];

but this did not work. So any alteration to this or different way of using the Array would be appreciated.

Thank you.

1
  • 1
    Without reading too much into it, for a start, $("#eventHighlight").text should be $("#eventHighlight").text(). Commented Apr 3, 2017 at 15:02

2 Answers 2

1

You can use json_encode() on php side to print the array directly into a variable in javascript:

var event = <?php echo json_encode($eventStartdates);?>;
Sign up to request clarification or add additional context in comments.

Comments

0

Replace:

var event = [$("#eventHighlight").text];

By:

var event = $("#eventHighlight").text.split(",");

Explanation:

$("#eventHighlight").text contain a string, and your event need an array. To do that, you need to split your string into array with split().

1 Comment

afraid this doesn't work. datepicker disappears altogether.

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.