SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2008
    Location
    South Florida
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can i use two AJAX requests?

    Hi, I just started learning AJAX techniques and im trying to do two things but only one works at a time

    the 1st is - i have two text fields:

    Code HTML4Strict:
    <tr>
    				<th class="white"><label for="plate">License Plate#:</label></th>
    				<td><input type="text" size=13 name="plate" id="plate" onKeyUp="ajaxPopID()" /></td>
    			</tr>
     
    			<tr>
    				<th class="white"><label for="portid">Port ID:</label></th>
    				<td><input type="text" size=10 name="portid" id="portid" /></td>
    			</tr>
    AJAX updates the second text field with a matching ID based on the License Plate number typed in the first text field, it works fine by itself but when i try the second AJAX function by click on a button farther down the page:

    Code HTML4Strict:
    <input type="button" onclick="ajaxFunction(); this.style.display='none'" name="verify" value="Verify"  id="button" class="btn" style='width:74px; height:26px;' />
    it doesnt do anything.

    this button calls the first AJAX request and makes another html table appear.
    I cant get both to work at the same time, is there away to use AJAX to ask for 2 different results and put them in two different places at different times?
    these are the AJAX functions im trying to make work:

    Code JavaScript:
    function ajaxFunction(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
     
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	//function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    		}
    	}
    	var plate = document.getElementById('plate').value;
    	var portid = document.getElementById('portid').value;
    	var dtn = document.getElementById('dtn').value;
     
    	if (document.checkform.cargo.checked == true) {
    		var cargo = 1;
    	} else { var cargo = 2; }
     
    	var queryString = "?plate=" + plate + "&portid=" + portid + "&dtn=" + dtn + "&cargo=" + cargo;
     
    	ajaxRequest.open("GET", "process_out.php" + queryString, true);
    	ajaxRequest.send(null);  
    }
     
    function ajaxPopID(){
    	var ajaxRequest2;  // The variable that makes Ajax possible!
     
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest2 = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest2 = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	//function that will receive data sent from the server
     
    	ajaxRequest2.onreadystatechange = function(){
    		if(ajaxRequest2.readyState == 4){
    			document.checkoutform.portid.value = ajaxRequest2.responseText;
     
    		}
    	}
    	var plateNum = document.getElementById('plate').value;
     
    	var queryString = "?plate=" + plateNum;
     
    	ajaxRequest2.open("GET", "ajaxPopIDprocess.php" + queryString, true);
    	ajaxRequest2.send(null);  
     
    }

  2. #2
    SitePoint Zealot Offspring2099's Avatar
    Join Date
    May 2005
    Location
    Los Angeles, CA
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not erroring out on these fields, is it. You have them, right?
    var dtn = document.getElementById('dtn').value;
    document.checkform.cargo.checked
    ajaxDisplay = document.getElementById('ajaxDiv')

  3. #3
    SitePoint Zealot Offspring2099's Avatar
    Join Date
    May 2005
    Location
    Los Angeles, CA
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also add "rand="+Math.random() to your urls in AJAX to avoid caching issues.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2008
    Location
    South Florida
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    no errors

    no i dont get any errors, i just click the button and nothing happens, but if i get rid of the ajaxPopID() function then the button will actually call the ajaxFunction and I'll get the results i want from that

  5. #5
    SitePoint Zealot Offspring2099's Avatar
    Join Date
    May 2005
    Location
    Los Angeles, CA
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have two forms? checkform and checkoutform or is that a mistake?


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
  •