Gayan Chaturanga
Gayan Chaturanga

Reputation: 57

Call two function() by one Onchange

im going to get some data from database by using Ajax. could you please let me know the way that i can trigger two function() by change a value of specified dropdown list using "OnChange"

note: most of the time system show only showhistory() function output

my existing codes

.............................................

<select name="po_no" onchange="showsize(this.value);showhistory(this.value);">

........................................................

 <script>

 function showhistory(str)
 {if (str=="")
   {
  document.getElementById("txtHistory").innerHTML="";
   return;
  } 
 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHistory").innerHTML=xmlhttp.responseText;
}
 }
xmlhttp.open("GET","gethistory2.php?s="+str,true);
xmlhttp.send();
 }
 </script>


 <script>
 function showsize(str)
 {
 if (str=="")
  {
   document.getElementById("sizeHint1").innerHTML="";
   return;
   } 
 if (window.XMLHttpRequest)
   {
   xmlhttp1=new XMLHttpRequest();
    }
 else
   {
   xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
   }
xmlhttp1.onreadystatechange=function()
   {
   if (xmlhttp1.readyState==4 && xmlhttp.status==200)
     {
    document.getElementById("sizeHint1").innerHTML=xmlhttp1.responseText;
    }
    }
  xmlhttp1.open("GET","getsize5.php?q="+str,true);
  xmlhttp1.send();
  }
 </script>

Upvotes: 1

Views: 4379

Answers (2)

S.K. Venkat
S.K. Venkat

Reputation: 1807

You are overriding the onChange's first value (showSize()) by reassigning the method showHistory(). To avoid this scenario, wrap it up into a single function which invokes both the calls as shown below :

function showAll(strValue) {

showAll(strValue);

showSize(strValue);

}

Finally call like as shown here :

<select name="po_no" onchange="showAll(this.value);">

Upvotes: 2

Amar Singh
Amar Singh

Reputation: 5622

call your second function on success of the first function

<select name="po_no" onchange="showhistory(this.value);">



<script>

 function showhistory(str)
 {if (str=="")
   {
  document.getElementById("txtHistory").innerHTML="";
   return;
  } 
 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHistory").innerHTML=xmlhttp.responseText;
    showsize(str);
}
 }
xmlhttp.open("GET","gethistory2.php?s="+str,true);
xmlhttp.send();
 }

 function showsize(str)
 {
 if (str=="")
  {
   document.getElementById("sizeHint1").innerHTML="";
   return;
   } 
 if (window.XMLHttpRequest)
   {
   xmlhttp1=new XMLHttpRequest();
    }
 else
   {
   xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
   }
xmlhttp1.onreadystatechange=function()
   {
   if (xmlhttp1.readyState==4 && xmlhttp.status==200)
     {
    document.getElementById("sizeHint1").innerHTML=xmlhttp1.responseText;
    }
    }
  xmlhttp1.open("GET","getsize5.php?q="+str,true);
  xmlhttp1.send();
  }
 </script>

Upvotes: 1

Related Questions