Jump to content

Multiple ajax requests


x1nick

Recommended Posts

Im still learning javascript at the moment, so bare with me on this.

 

I need to make 2 ajax requests when a user presses a button, which update two separate divs.

 

My ajax file (any constructive opinions on my class would be appreciated too! don't know how good it actually is)

function ajaxclass () {
  var xmlhttp;
  this.request = request;

  function GetXmlHttpObject() {
    if (window.XMLHttpRequest)   {
      return new XMLHttpRequest();
    }
    if (window.ActiveXObject("Msxml2.XMLHTTP")) {
      return new ActiveXObject("Msxml2.XMLHTTP");
    }
    if (window.ActiveXObject("Microsoft.XMLHTTP")) {
      return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
  }  
  
  function request(url,loaded,loading) {    
    xmlhttp = GetXmlHttpObject();
    if (xmlhttp == null) {
      alert ("Browser does not support HTTP Request");
      return;
    }  
    xmlhttp.onreadystatechange = function() {
      switch(xmlhttp.readyState) {
        case 0: // Uninitialized
          break;
        case 1: // Loading
          if (loading != "") {
            eval(loading);
          }
          break;
        case 2: // Loaded
          break;
        case 3: // Interactive
          break;
        case 4: // Done!
          response = xmlhttp.responseText;
          eval(loaded);
	  break;
        default:
          break;
      }  
    }
    xmlhttp.open("GET",url,true);  
    xmlhttp.send(null);   
  }
  
}

var ajax = new ajaxclass();

 

It does work, but I have a problem when carrying out 2 requests.

The button runs this function

 

<script type="text/javascript">
  function doupdate (tplid) {
    var result = confirm('Are you sure you want to update {tplname}?');
    if (result) {
      openmessage();
      ajax.request('index.php?s=ajax&acms&amod=tpl&ajax=update&tplid='+tplid, 'document.getElementById(\'messagearea\').innerHTML = response;','');
      ajax.request('index.php?s=ajax&acms&amod=tpl&ajax=results', 'document.getElementById(\'filterreslts\').innerHTML = response;','');
    }
  }
</script>

 

Now firebug says both run, and both scripts return a response. The 2nd ajax request dispalys a result, but the first dosen't.

If the 2nd request is removed the first one works, and displays the message in the correct area.

 

Why is it when both are run, that the message is not displayed but firebug reports it as returning the required result?

 

PS openmessage() simply sets a div to display.style = 'block' which works, but the div is empty.

Link to comment
https://forums.phpfreaks.com/topic/170510-multiple-ajax-requests/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.