将数据库值存储在javascript数组

时间:2015-05-22 10:49:06

标签: javascript php jquery html arrays

我正在制作一种预订页面,您可以在此预订特定日期的位置,所有日期都将存储在每个位置的数据库中。 但我希望从数据库中获取所有日期并将它们存储在javascript数组中,以便我可以从页面上的datepicker中禁用这些日期。 我知道如何使用php和mysql选择日期,但我无法想出一种将数据存储在javascript数组中的方法。

这是datepicker的js代码:

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
    });
}); 

我希望数组保存数据库中的日期。

4 个答案:

答案 0 :(得分:1)

您需要从数据库和json_encode他们

获取数据

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

答案 1 :(得分:0)

您无法直接在PHP脚本中编写JS代码。 PHP在服务器上运行,JS在客户端上运行。

从PHP的角度来看,JS就像HTML一样。

你可以做什么,将其输出到标签。

类似($ date包含禁用日期):

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

在HTML Body输出结束之前调用此函数。

答案 2 :(得分:-1)

这只是你的js代码,也显示了一些PHP。

如果你没有使用任何花哨的ajax,你可以使用这样的东西:

<?php 
$dates = "";

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

答案 3 :(得分:-1)

您有

等选项
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
相关问题