Tyler Crompton
Tyler Crompton

Reputation: 12652

$_GET values are not set during AJAX query

Basically what I want to happen is for the calendar to go change months upon clicking one of the arrows. I feel like there is a simple error somewhere and that I am just overlooking it. Just let me know if I need to explain something.

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();
}

Upvotes: 0

Views: 292

Answers (2)

cortices
cortices

Reputation: 373

@Mike: true, GET requests do not work to OS paths, only to server paths.

Anyway, you should always use absolute paths in situations like these, it's safer:

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

Upvotes: 0

Mike Lewis
Mike Lewis

Reputation: 64137

Making a GET request to an OS path seems weird to me. How about just making a request to http://localhost/calendar.php?month=4&year=2011 (or whatever you are running your server as).

This would make most sense because the web server is the one that sets the $_GET variables, and that webserver is usually listening to port 80. When you make this call, I'm fairly sure no webserver is listening.

Upvotes: 4

Related Questions