我正在使用 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 = '上个月';
我的所有 calendar.php 文件都在这里:
致谢,非常感谢任何帮助。
I am creating an events calendar in a WordPress theme using David Walsh's calendar function. I want to move between months with AJAX but am unfamiliar and having trouble posting and getting the month and year variables. I am referencing this AJAX video tutorial.
At the top of the page template, I have this function:
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();
});
}
In calendar.php:
$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int) ($_GET['year'] ? $_GET['year'] : date('Y'));
$contentVar = $_POST['month']['year'];
And my previous/next month controls look like this:
$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>';
All of my calendar.php file here: http://pastebin.com/R9zpA3yM
Thanks in advance, any help much appreciated.
发布评论
评论(1)
您似乎没有正确设置月份和年份。您将“月”和“年”作为字符串传递,然后没有将它们设置为传递到 PHP 脚本的正确参数名称。试试这个:
然后在你的 PHP 中:
我不会使用
onmousedown
,而是只在onclick
中使用该函数,只需确保它在我的函数末尾返回 false 即可已包括在内。我不确定您的 PHP 脚本中如何使用月份、年份和 contentVar,因此以下是一个假设,但看起来您只需要以下内容:
然后相应地使用这些变量来获取数据?
It doesn't look like you're setting the month and year correctly. You're passing in "month" and "year" as strings and then not setting them as the right parameter names to pass into your PHP script. Try this:
Then in your PHP:
Rather than use
onmousedown
I would just have the function in theonclick
, just make sure it returns false at the end of the function which I've included.I am not sure how month, year and contentVar are all being used in your PHP script, so the following is an assumption, but it looks like you would just need something along the lines of:
And then use those variables accordingly to get your data?