SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Cape Town
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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...

    Code:
    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

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Cape Town
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jim! Thanks so much for the reply! 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...

    Code:
    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...

    Code:
    <?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?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?)

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Cape Town
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jim. $_GET is used to retrieve parameters sent via the URL of the page. For example
    Code:
    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.

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That program recieves a GET request, so better use this instead:
    Code:
      xmlhttp.open('GET', url + '?' + params); 
      xmlhttp.send('');

  8. #8
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Cape Town
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

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

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,834
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by jimfraser View Post
    Also, expect xmlhttprequest to cache the responses you receive back if you use GET.
    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.

  11. #11
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Won't using header('Cache-Control: no-cache');
    Maybe. I've never come up with a good reason to not use POST though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •