ajax script select box is repeated

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kkshansid
    New Member
    • Oct 2008
    • 232

    ajax script select box is repeated

    in ajax script
    select box is repeated
    state 2 times
    district 2 times
    kindly suggest me to correct this error

    Code:
    <html>
    <head>
    <script languphase="javascript" type="text/javascript">
    var xmlhttp;
    
    function showUser(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url="getuser.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    </script>
         <?php require("conn.php");?>
    </head>
    <body>
    
    <?php
    //Select a User:
    
    $q=$_GET["q"];
    if($q=="")
    {
    $q="assam";
    }
    
    $sql="SELECT distinct(state) FROM statedistrict";
    
    $result = mysql_query($sql);
    ?>
    
    <select name="users" onchange="showUser(this.value)">
    <option value="<?php echo $q; ?>"><?php echo $q; ?></option>
    <?php 
    while($row = mysql_fetch_array($result))
      {  ?>
     <option value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
     <?php }
    
    
    ?> 
    </select>
    
    
    <br />
    <div id="txtHint"><b>Person info will be listed here.</b></div>
    <select name="TR_District" id="TR_District">
    <?php
    
    
    
    $sql="SELECT district FROM statedistrict WHERE state = '".$q."'";
    
    $result = mysql_query($sql);
    
    
    
    while($row = mysql_fetch_array($result))
      {
      ?>
     <option value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
     <?php }
    
    
    ?> 
    </select>
    </form>
    </body>
    </html>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    in your callback function stateChanged() you set the innerHTML of the node txtHint

    Code:
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    ... this node doesn't contain anything but text and a <br/> tag which is then replaced ... so all other nodes on the page will stay as they are ...

    kind regards

    Comment

    • kkshansid
      New Member
      • Oct 2008
      • 232

      #3
      thanx for ur valuable suggestion it worked

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        no problem :) glad to hear that you got it working ....

        kind regards
        gits

        Comment

        Working...