Jump to content

I Need Help Combining Functions


Schrene

Recommended Posts

I am still learning.

How would I combine these?

Or not repeat  "The variable that makes Ajax possible" part of the code.

function showID(){
   var idRequest;  // The variable that makes Ajax possible!
   
   try{
      // Opera 8.0+, Firefox, Safari
      idRequest = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         idRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            idRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   

   idRequest.onreadystatechange = function(){
      if(idRequest.readyState == 4){
         var idDisplay = document.getElementById('color_id');
         idDisplay.innerHTML = idRequest.responseText;
      }
   }
   var color = document.getElementById('color').value;
   
   var queryString = "?color=" + color;
   idRequest.open("GET", "colorfil_data_color_id.php" + queryString, true);
   idRequest.send(null);
}
function showName(){
   var nameRequest;  // The variable that makes Ajax possible!
   
   try{
      // Opera 8.0+, Firefox, Safari
      nameRequest = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         nameRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            nameRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   

   nameRequest.onreadystatechange = function(){
      if(nameRequest.readyState == 4){
         var nameDisplay = document.getElementById('color_name');
         nameDisplay.innerHTML = nameRequest.responseText;
      }
   }
   var color = document.getElementById('color').value;
   
   var queryString = "?color=" + color;
   nameRequest.open("GET", "colorfil_data_color_name.php" + queryString, true);
   nameRequest.send(null);
}
function showNum(){
   var numRequest;  // The variable that makes Ajax possible!
   
   try{
      // Opera 8.0+, Firefox, Safari
      numRequest = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         numRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            numRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   

   numRequest.onreadystatechange = function(){
      if(numRequest.readyState == 4){
         var numDisplay = document.getElementById('color_num');
         numDisplay.innerHTML = numRequest.responseText;
      }
   }
   var color = document.getElementById('color').value;
   
   var queryString = "?color=" + color;
   numRequest.open("GET", "colorfil_data_color_num.php" + queryString, true);
   numRequest.send(null);
   
}

 

Thanks in advance for your help :D

Link to comment
https://forums.phpfreaks.com/topic/226486-i-need-help-combining-functions/
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.