0

我一直在搜索这个很棒的信息网站,但还没有找到我要找的东西,只有零碎的东西。

我对整个 JavaScript 语言/jQuery 库相当陌生,但我开始越来越喜欢它,因为你可以用它做一些事情。

我有一个文件,array.php,它打印出带有某些日期的 json 格式变量,我想在 .datepicker 日历中禁用这些日期。

除了来自 sql 部分的变量之外,我已经完成了整个工作。

这有效:var $BadDates = new Array("2012-05-28","2012-05-29","2012-05-30");

我想要的是这样的:

$.getJSON('array.php', function(data) {
var $BadDates = [];
}

我的array.php:

$query = "SELECT * FROM dates";
$query_result = mysql_query($query) or die(mysql_error());

$results = array();


while($row = mysql_fetch_assoc($query_result)) {



  $n["startdate"] = $row['startdate'];


  array_push($results, $n); 
}
print json_encode($results);

这就是我想要完成的(我知道这一切都错了,只是为了展示我的想法):

$BadDates = new Array("$.getJSON('array.php')");

一直在检查许多解决方案,但没有任何运气......

我很感激我能得到的所有帮助!

/奥斯卡

4

1 回答 1

0

这里不需要使用 AJAX,尽管我会向您展示两种方式:

首先,由于您的 PHP 文件正在打印 JSON 编码的字符串,您可以在 PHP 生成的页面中的必要位置包含它:

<?php
    /* Some php */
?>

<!-- some html -->

<script type="text/javascript">
    var badDates = <?php include('array.php'); ?>;
    /* do something with badDates, which is now a JS array */
</script>

<!-- some more html -->

<?php
    /* Some more php */
?>

它会输出类似:http ://codepad.org/4xHlcwig

对于 AJAX 检索的数组,您将:

<script type="text/javascript">
    $.ajax({
        url: '/array.php',
        dataType: 'json',
        success: function (data) {
            /* data contains a JS array which was outputted from your file and decoded by jQuery for you */
        }
    });

    /* -OR- your shorthand method */

    $.getJSON('array.php', function(data) {
        /* data contains a JS array which was outputted from your file and decoded by jQuery for you */

    }
</script>
于 2012-05-25T22:58:46.623 回答