Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am making a kind of reservation page where you can reserve a location for a specific day, all the dates will be stored in a database for each location. But I want to get all the dates from the database and store them in a javascript array so I can disable these dates from the datepicker on the page. I know how to select the dates using php and mysql but I can't figure out a way to store the data in a javascript array.

This is the js code for the datepicker:

var disableddates = ["20-05-2015", "12-11-2014", "21-05-2015", "12-20-2014"];

function DisableSpecificDates(date) {
    var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
    return [disableddates.indexOf(string) == -1];
}
$(function() {
    $("#date1").datepicker({
    beforeShowDay: DisableSpecificDates
    });
}); 

I want the array to hold the dates from the database.

share|improve this question
    
make an ajax call to get data fron your database then push it to the array disableddates. – Suchit May 22 '15 at 10:52

You need to fetch data from database and json_encode them

var disableddates = <?php echo json_encode($response)?> ;

share|improve this answer

You can't write JS code directly in you PHP script. PHP runs on the server, JS runs on the client.

JS from PHP's point of view is just like HTML.

What you can do, is output this to tag.

Something like ($dates contain the disabled dates):

function outputDatePickerScript($dates) {
    echo 'var disableddates = ["' . implode('","', $dates) . '"]';
    echo .... // Rest of the JS script
}

Call this function before the end of your HTML Body output.

share|improve this answer

You have options like

1.to write jquery function in php scipt echo '<script></script' and store the date values in array by select & fetch statement e.g.

    /*Your select statement for dates*/
    while loop{

        $dates .= '"'.$row["date"].'",'
    }
    $dates      = rtrim($dates, ",");

    var disableddates = ['.$dates.'];

2> Store db values in a hidden field on front html with an id and pass the value in javascript varaible by selecting id
share|improve this answer

That's only your js code, show a little of the php too.

if you're not using any fancy ajax you could use something like this:

<?php 
$dates = "";

while () { //this while would be your database while
    $dates .= '"'.$row["date"].'",';
}
$dates      = rtrim($dates, ",");
?>
var disableddates = [<?php echo $dates; ?>];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.