【问题标题】:Move through PHP calendar months with jQuery AJAX使用 jQuery AJAX 浏览 PHP 日历月
【发布时间】:2011-11-17 23:41:09
【问题描述】:

我正在使用 David Walsh's calendar 函数在 WordPress 主题中创建活动日历。我想在使用 AJAX 的月份之间移动,但我不熟悉并且无法发布和获取月份和年份变量。我引用了这个AJAX video tutorial

在页面模板的顶部,我有这个功能:

    function swapContent(month,year) {
        jQuery("#calendar").html("<img src='images/ajax-loader.gif'>").show();
        var url="calendar.php";
        jQuery.post(url, {contentVar: month, year}, function(data){
            jQuery("#calendar").html(data).show();
        });
    }

在calendar.php中:

    $month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
    $year = (int)  ($_GET['year'] ? $_GET['year'] : date('Y'));

    $contentVar = $_POST['month']['year'];

我的上/下个月控件如下所示:

$previous_month_link = '&lt;a href="?month='.($month != 1 ? $month - 1 : 12).'&amp;year='.($month != 1 ? $year : $year - 1).'" class="control" onClick="return false" onmousedown="javascript: swapContent("month", "year")"&gt;Previous Month&lt;/a&gt;';

我所有的 calendar.php 文件都在这里:http://pastebin.com/R9zpA3yM

提前致谢,非常感谢任何帮助。

【问题讨论】:

    标签: php jquery ajax calendar


    【解决方案1】:

    您似乎没有正确设置月份和年份。您将“月”和“年”作为字符串传递,然后没有将它们设置为正确的参数名称以传递到您的 PHP 脚本中。试试这个:

    function swapContent(monthVal,yearVal) {
        jQuery("#calendar").html("<img src='images/ajax-loader.gif'>").show();
        var url="calendar.php";
        jQuery.post(url, {month: monthVal, year: yearVal}, function(data){
            jQuery("#calendar").html(data).show();
        });
        return false;
    }
    

    然后在你的 PHP 中:

    $monthVal = ($month != 1 ? $month - 1 : 12);
    $yearVal = ($month != 1 ? $year : $year - 1);
    
    $previous_month_link = '<a href="?month='.$monthVal.'&year='.$yearVal .'" class="control" onClick="swapContent("'.$monthVal.'","'.$yearVal.'");">Previous Month</a>';
    

    与其使用onmousedown,不如使用onclick 中的函数,只要确保它在我包含的函数末尾返回false。

    我不确定您的 PHP 脚本中如何使用月份、年份和 contentVar,因此以下是一个假设,但看起来您只需要以下内容:

    $month = (int) ($_POST['month'] ? $_POST['month']
                                    : ($_GET['month'] ? $_GET['month'] 
                                                      : date('m')));
    
    $year= (int) ($_POST['year'] ? $_POST['year']
                                   : ($_GET['year'] ? $_GET['year'] 
                                                      : date('Y')));
    

    然后相应地使用这些变量来获取您的数据?

    【讨论】:

    • 谢谢!仍然没有让它工作。这是我的 calendar.php 文件:pastebin.com/R9zpA3yM。我注意到控件在&lt;form&gt;&lt;input&gt; 标签(第 148 行)内回显到页面上 - 我将它们设为单数链接,但仍然没有骰子。另外,对于上个月和下个月的导航,我需要prevMonthValnextMonthVal 的变量吗?再次感谢。
    • 哦,别管&lt;input&gt;。我也在控制台中收到此错误:Uncaught SyntaxError: Unexpected token : :8888/onlyinpgh/calendar/?month=8&amp;year=2011:11。谢谢。
    • 哦,是的,您的链接可能需要引用 prevMonthValnextMonthVal,正如您所描述的。不确定您的 javascript 错误,它表明它不期望​​ :.查看后面引用的行,可能是末尾的 2011:11,它是 URL 中的非法字符, : 应该编码为 %3A
    猜你喜欢
    • 1970-01-01
    • 2014-09-29
    • 2017-11-17
    • 2014-07-08
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多