AJAX查询时没有设置$_GET值

$_GET values are not set during AJAX query

本文关键字:GET 设置 查询 AJAX      更新时间:2023-09-26

基本上,我想要发生的是,在单击其中一个箭头时,日历会更改月份。我觉得有一个简单的错误,我只是忽略了它。如果我需要解释什么,请告诉我。

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='"#'">&lt;</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='"#'">&gt;</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();
}

向操作系统路径发出GET请求对我来说似乎很奇怪。如果只是发出请求http://localhost/calendar.php?month=4&year=2011(或任何您正在运行您的服务器)。

这将是最有意义的,因为web服务器是设置$_GET变量的服务器,并且该web服务器通常侦听端口80。当你打这个电话时,我很确定没有web服务器正在监听。

@Mike: true, GET请求对操作系统路径不起作用,只能对服务器路径起作用

无论如何,在这样的情况下你应该总是使用绝对路径,这样更安全:

xmlhttp.open("GET", "http://localhost/home2/crompton/scripts/calendar.php?month=" + month + "&year=" + year, true);