0

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.

1
  • make an ajax call to get data fron your database then push it to the array disableddates. Commented May 22, 2015 at 10:52

4 Answers 4

1

You need to fetch data from database and json_encode them

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

-1

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

Comments

-1

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; ?>];

Comments

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.