Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.