Select and Onchange Event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Olamy
    New Member
    • Sep 2013
    • 2

    Select and Onchange Event

    I have 3 input text boxes to fill using values populated from my database.

    I am using an onchange event to put my selection into the first input text box.

    My problem starts when I try to add value to the second input box.

    I loose the first value but the second remain.

    How will I arrange for the onchange event to fire successfully three times?

    Please see some of the codes below.

    Code:
    function useAjax_1()
    {
      myrnd=parseInt(Math.random()*999999999999999);
      var str=document.getElementById("pcode").value;
    
      var xmlhttp;
    
      if (str.length==0)
      {
        y1.innerHTML="" ;
        return;
      }
      x1=document.getElementById("sch1");
      y1=document.getElementById("sch11");
      x1.style.background="yellow";
    
      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)
        {
          y1.innerHTML=xmlhttp.responseText;
        }
      }
      var queryString = "?url=" + str ;
         queryString += "&rnd=" + myrnd;
    
      xmlhttp.open("GET","schools.php" + queryString,true);
      xmlhttp.send();
    }
    		
    function showtext(t)
    {
      x1.value= t;
      x1.value=x1.value.toUpperCase();
    }
    y1.innerHTML="";
    
    </script>
    ----from the database

    Code:
    $j=0; 
    while($row=mysqli_fetch_assoc($result1))
    {
      foreach($row as $sch_name => $value)
      {
        $rowcat1[$j]['sch_name']=$value;	
      }
      $j++;
    }
    echo "<select id='schsel1' name='schsel1' onchange = 'showtext(this.value)'>";
    for($j=0; $j < count($rowcat1); $j++)
    {
      echo"<option value = '{$rowcat1[$j]['sch_name']}'> 
      {$rowcat1[$j]['sch_name']}<br />";
    }	//sizeof($rowcat)
    echo "</select> <br />";
    -------

    Code:
    <label for= "lblsch1">First Pick up School</label>
    <input id= "sch1" name="sch1" size = "58" maxlength = "85"
     onclick = "useAjax_1()" <value=<? echo @$_POST['sch1']?>>
    <br />
    
    <label for= "lbl_sch1">&nbsp;</label>
    <span id = "sch11" type = "text" size = "85" ></span>
    <br />
    
    <label for= "lbl_sch1">Second School</label>
    <input id= 'sch2' name="sch2" size = "58" maxlength = "85"
    onclick = "useAjax_2()" <value = <?php echo @$_POST['sch2'] ?>>
    <br />
    
    <label for= "lbl_sch1">&nbsp;</label>
    <span id = "sch22" type = "text"size = "40" maxlength = "65"></span>
    <br />
    				
    <label for= "lbl_sch1">Third School</label>
    <input id= 'sch3' name="sch3" size = "58" maxlength = "85"
    onclick = "useAjax(3)"<value = <?php echo @$_POST['sch3'] ?>>
    <br />
    <label for= "lbl_sch1">&nbsp;</label>
    <span id = "sch33" type = "text" size = "58" maxlength = "85"></span>
    <br />
    Last edited by Dormilich; Sep 15 '13, 09:51 AM. Reason: Please use code tags when posting code or formatted data
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the x1 in showtext() is undefined (x1 is a local variable of useAjax_1()).

    are useAjax_2() and useAjax() defined anywhere?

    the <label>’s for attribute must exactly match another element’s id.

    <input>: invalid attribute <value

    Comment

    • Olamy
      New Member
      • Sep 2013
      • 2

      #3
      Thanks for all your help. I have gone through the codes again and rewrote it all. The problem had been resolved.

      Thanks again

      Comment

      Working...