Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a php page(calendar.php) which is using javascript to load a php calendar(calendar_start.php) and display the calendar in the center of the page.

I am trying to obtain a variable on the calendar_start.php page, however I cannot access variable which I am able to access from the calendar.php page.

below is one of the scripts used on calendar.php to generate canendar_start.php:

<script language="javaScript" type="text/javascript">
function initialCalendar(){
    var hr = new XMLHttpRequest();
    var url = "calendar_start.php";
    var currentTime = new Date();
    var month = currentTime.getMonth() + 1;
    var year = currentTime.getFullYear();
    showmonth = month;
    showyear = year;
    var vars = "showmonth="+showmonth+"&showyear="+showyear;
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("showCalendar").innerHTML = return_data;
        }
    }
    hr.send(vars);
    document.getElementById("showCalendar").innerHTML = "processing...";
}
</script>

and below is part of the page calendar.php where I am generating the calendar:

<body onLoad="initialCalendar(<?php $_GET['page']; ?>);">

I am unable to grab the variable on calendar_start.php and I would really appreciate any help.

Kindest Regards, Ryan

EDIT::

This may be helpful, this is the error when I am trying to display the variable on calendar_start.php

Notice: Undefined index: page in C:\wamp\www\project\calendar_start.php

share|improve this question
Which one is the variable you're trying to grab? – tomor Apr 13 at 22:31
$page, which is in the URL of calendar.php and I can grab in calendar.php using this: $page = $_GET['page']; – Ryan Apr 13 at 22:32
You're sending a POST request, and trying to use GET on the PHP side? Try $_POST? – Datsik Apr 13 at 22:34
I've tried using $_POST and it does not work – Ryan Apr 13 at 22:36

1 Answer

up vote 0 down vote accepted

Change $_GET['page'] to $_REQUEST['page'].

$_REQUEST combines $_GET and $_POST together (with $_POST taking precedence). That way you won't have to worry about backward comaptibility if you change it in the future.

It is, however, a bad idea to echo ANY of these variable unsanitized to users browser.

EDIT:

Although your main problem is that you forgot the "echo" command in front of the variable.

share|improve this answer
Hi, I have tried using $_REQUEST however it does not work, I have also tried using echo and this wont work still. – Ryan Apr 13 at 22:43
Have you tried both? Try changing $_GET to $_POST or $_REQUEST and putting in echo. – JRL Apr 13 at 22:45
I have, but can you please give an example to be sure I'm doing it right? – Ryan Apr 13 at 22:47
<?php echo (isset($_POST['page'])) ? $_POST['page'] : ''; ?> – JRL Apr 13 at 22:50
with that calendar_start.php fails to load, no error message is displayed.. – Ryan Apr 13 at 22:53
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.