基本上我想要发生的是日历在点击其中一个箭头后改变几个月。我觉得某处有一个简单的错误,我只是忽略它。如果我需要解释一下,请告诉我。
calendar.php:
<?php
function calendar($selectedDate = "now") {
$selectedDate = strtotime($selectedDate);
$theFirst = strtotime(date('m/01/Y', $selectedDate));
$weekDayOfTheFirst = (int) date('w', $theFirst);
$lastDayOfTheMonth = (int) date('j', strtotime('-1 day', strtotime('+1 month', $theFirst)));
$numberOfWeeks = ceil(($weekDayOfTheFirst + $lastDayOfTheMonth) / 7);
$selectedMonth = (int) date('m', $selectedDate);
echo "<table id=\"calendar\"><tr><td onclick=\"updateCalendar('";
echo date("F", strtotime('-1 month', $selectedDate)) . "', '" . date("Y", strtotime('-1 month', $selectedDate));
echo "')\"><a href=\"#\"><</a></td><th colspan=\"5\">" . date("F 'y", $selectedDate) . "</th><td onclick=\"updateCalendar('";
echo date("F", strtotime('+1 month', $selectedDate)) . "', '" . date("Y", strtotime('+1 month', $selectedDate));
echo "><a href=\"#\">></a></td></tr>";
echo "<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>R</th><th>F</th><th>S</th></tr>";
for ($week = 0; $week < $numberOfWeeks; $week++) {
echo "<tr>";
for ($dayOfTheWeek = 0; $dayOfTheWeek <= 6; $dayOfTheWeek++) {
$date = strtotime($dayOfTheWeek + $week * 7 - $weekDayOfTheFirst . 'days', $theFirst);
$dayOfTheMonth = (int) date('j', $date);
echo "<td" . (date('m/Y', $date) == date('m/Y', $selectedDate) ? "" : " class=\"outOfMonth\"");
echo ($date == $selectedDate ? " id=\"today\"" : "") . "><a href=\"#\">" . $dayOfTheMonth . "</a></td>";
}
echo "</tr>";
}
echo "</table>\n";
}
echo "<div>" . $_GET["month"] . "</div><div>" . $_GET["year"] . "</div>";
if (isset($_GET["month"]) && isset($_GET["year"])) {
echo "<div>GET</div>";
calendar($_GET["month"] . ' ' . $_GET["year"]);
} else {
echo "<div>not GET</div>";
calendar();
}
?>
calendar.js:
function updateCalendar(month, year) {
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("sideBar").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "/home2/crompton/scripts/calendar.php?month=" + month + "&year=" + year, true);
xmlhttp.send();
}
答案 0 :(得分:4)
向OS路径发出GET请求对我来说似乎很奇怪。如何向http://localhost/calendar.php?month=4&year=2011(或者您正在运行服务器的任何内容)发出请求。
这最有意义,因为Web服务器是设置$ _GET变量的服务器,并且该Web服务器通常正在侦听端口80.当您进行此调用时,我非常确定没有Web服务器正在侦听。
答案 1 :(得分:0)
@Mike:true,GET请求不适用于OS路径,只适用于服务器路径。
无论如何,你应该总是在这样的情况下使用绝对路径,它更安全:
xmlhttp.open("GET", "http://localhost/home2/crompton/scripts/calendar.php?month=" + month + "&year=" + year, true);
答案 2 :(得分:-1)
为什么不使用jQuery并将浏览器的互操作性留给其他人?此外,post在大多数情况下都比获取更好,urlencoding值非常难看。
function updateCalendar(upd_month, upd_year){
$.post("/home2/crompton/scripts/calendar.php",
{
month: upd_month,
year: upd_year
},function(data){
//handle successful response here.
}
);
}
如果使用此方法,则必须将php $ _GET切换为$ _POST。