Dropdown select onchange to update multiple divs

I have a selectbox with an onchange event to update content with content from another file. It works fine. but what I want to do is update the content in two different divs at the same time when I select something from my selectbox.

So I added another function similar to the first one and added it to onchange in the tag.

The problem is that it still only updates one of the two. What am I doing wrong? Or is there an easier way to update the two at the same time with information from another page?

script on main page:

<script>
function showFirstDiv(str) {
  if (str=="") {
    document.getElementById("div1").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("div1").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","infopage.php?info_div1="+str,true);
  xmlhttp.send();
}

function showSecondDiv(str) {
  if (str=="") {
    document.getElementById("div2").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("div2").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","infopage.php?info_div2="+str,true);
  xmlhttp.send();
}

      

html on the main page

<div id="users">
  <form>
    <select name="users" onchange="showFirstDiv(this.value); showSecondDiv(this.value);">
      <option value="">Select a person:</option>
      <option value="1">Peter Griffin</option>
      <option value="2">Lois Griffin</option>
      <option value="3">Joseph Swanson</option>
      <option value="4">Glenn Quagmire</option>
    </select>
  </form>
</div>

<!-- div1 -->
<div id="div1">
  <p>info to be listed in div 1 here</p>
</div>

<!-- div2 -->
<div id="div2">
  <p>info to be listed in div 2 here</p>
</div>

      

infopage.php

<?php

if ($_GET[info_div1] == '1')
    {
        echo 'info 1 to be showed in div 1';
    }
if ($_GET[info_div1] == '2')
    {
        echo 'info 2 to be showed in div 1';
    }
if ($_GET[info_div1] == '3')
    {
        echo 'info 3 to be showed in div 1';
    }
if ($_GET[info_div1] == '4')
    {
        echo 'info 4 to be showed in div 1';
    }

if ($_GET[info_div2] == '1')
    {
        echo 'info 1 to be showed in div 2';
    }
if ($_GET[info_div2] == '2')
    {
        echo 'info 2 to be showed in div 2';
    }
if ($_GET[info_div2] == '3')
    {
        echo 'info 3 to be showed in div 2';
    }
if ($_GET[info_div2] == '4')
    {
        echo 'info 4 to be showed in div 2';
    }

?>

      

+3


source to share


1 answer


Both functions use the same global variable xmlhttp

. Therefore, when you call the second function, it will overwrite this variable with its XMLHttpRequest object. You must use a local variable by declaring it with var

.

In general, you should always declare variables as local unless you really need global variables.



function showFirstDiv(str) {
  var xmlhttp;
  if (str=="") {
    document.getElementById("div1").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("div1").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","infopage.php?info_div1="+str,true);
  xmlhttp.send();
}

function showSecondDiv(str) {
  var xmlhttp;
  if (str=="") {
    document.getElementById("div2").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("div2").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","infopage.php?info_div2="+str,true);
  xmlhttp.send();
}

      

+2


source







All Articles