AJAX xmlhttp.send() not working

Hi everyone

I’m a complete and utter AJAX noob and have been sitting with the same problem for the last 3 hours. I’m trying to display a dynamically generated calendar (generated via PHP using a database). But before I continue, here’s my javascript…

function loadURL(url){
  var params = "year=2007&month=8";

  try {
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {
    alert(e);
  }

  xmlhttp.onreadystatechange = function(){
    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
      // xmlhttp.responseText object contains the response.
      document.getElementById("calDiv").innerHTML = xmlhttp.responseText;
    }
    else{
      document.getElementById("calDiv").innerHTML = "LOADING";
    }
  };

  xmlhttp.open('POST', url);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.send(params);

}

The calendar does display when the loadURL function is called, but doesn’t display using the paramaters defined in the “params” variable, it defaults to the current month (that’s part of my php script, so it’s supposed to do that). So, it seems to me that xmlhttp.send(params) isn’t actually sending the desired paramaters.

Can anyone please help me with this, as I am running short on hair. Thanks :slight_smile:

looks ok…

if you go directly to the php page using your browser with http://mypage.php?year=2007&month=8 does it show the correct thing?

Hi Jim! Thanks so much for the reply! :slight_smile: Yes, I have tested it like that, and it works fine. The script was originally written in PHP (with page reloads) and the AJAX was kind of an afterthought. If I change the script as follows…

xmlhttp.open('POST', url); 

it works just fine. But I can’t for the life of me get the xmlhttp.open(params) statement to work.

Let me paste my PHP calendar script, so you can see what it does…

<?php
  include('includes/class.lib.calendar.php');

  $y = date("Y");
  $m = $_GET['month'];

  if($m == ""){
    $m = date("m");
  }

  $pM = $m - 1;
  $nM = $m + 1;

  if($nM > 12){
    $nM = 1;
  }

  if($pM < 1)
  {
    $pM = 12;
  }

  $Calendar = new Calendar;
  $Calendar->Initialize($m, $y);
  $Calendar->SetMonth();
  $Calendar->GetEventDates();
  $table = $Calendar->PrintCalendar();
  $events = $Calendar->GetEventInfo();

  echo $table;

?>

So as you can see, the PHP script basically returns a table, but requires the paramaters “year” and “month” to be posted. Any ideas?

I don’t do much PHP but doesn’t the $_GET variable contain stuff sent using the GET method? Shouldn’t you use $_POST for stuff sent via POST? (or er $_REQUEST for either I think, isnt it?)

Hi Jim. $_GET is used to retrieve parameters sent via the URL of the page. For example

http://www.mysite.com/index.php?name=tom&surname=harrison

.

$_GET[‘name’] contains “Tom” and $_GET[‘surname’] contains Harrison in this particular instance.

With my previous post I mean to say that I took out the xmlhttp.send(params) completely and simply changed xmlhttp.open(“POST”, url) to xmlhttp.open(“POST”, “calendar.php?year=2007&month=8”) and it loaded everything correctly, so I don’t think there’s anything wrong with the PHP anyway. For some reason however, xmlhttp.send(params) is still not sending the required variables to the calendar.php file.

As you say, $_GET is used to retrieve variables sent with the URL for example:

http://mypage?id=1&thing=2

When you send a request for a page in that format, I would expect $_GET to work fine (no matter if the request you are sending is a POST) as you have illustrated here:
xmlhttp.open(“POST”, “calendar.php?year=2007&month=8”);
xmlhttp.send(“whatever, it doesn’t matter”);

But if you want to send the variables via POST, ie
xmlhttp.open(“POST”, “calendar.php”);
xmlhttp.send(“year=2007&month=8”);

you will need to use the $_POST collection server side.

That program recieves a GET request, so better use this instead:

  xmlhttp.open([COLOR="brown"]'GET'[/COLOR], url + [COLOR="brown"]'?'[/COLOR] + params);
  xmlhttp.send([COLOR="brown"]''[/COLOR]);

If you are sending short amounts of data and security doesn’t matter then by all means use GET. Otherwise, POST.

Also, expect xmlhttprequest to cache the responses you receive back if you use GET.

Thanks for your help guys! I clearly misunderstood the workings of the script, but I finally got it working using your teachings :wink: Thanks again!

My limited understanding is that IE will cache the responses. Not sure if Firefox does.

Won’t using header(‘Cache-Control: no-cache’); in the PHP prevent the caching?

This is something that people should know because it can really screw with your debugging.

Won’t using header(‘Cache-Control: no-cache’);

Maybe. I’ve never come up with a good reason to not use POST though.