繁体   English   中英

使用jQuery AJAX在PHP日历月份中移动

[英]Move through PHP calendar months with jQuery AJAX

我正在使用David Walsh的日历功能以WordPress主题创建事件日历。 我想在使用AJAX的月份之间移动,但不熟悉并且在发布和获取月份和年份变量时遇到麻烦。 我正在参考此AJAX视频教程

在页面模板的顶部,我具有以下功能:

    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 = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control" onClick="return false" onmousedown="javascript: swapContent("month", "year")">Previous Month</a>';

我在此处的所有calendar.php文件: http : //pastebin.com/R9zpA3yM

在此先感谢您的帮助。

您似乎没有正确设置月份和年份。 您以字符串形式传递“ month”和“ year”,然后没有将它们设置为传递给您的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脚本中如何使用month,year和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')));

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM