Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiple Dropdown Menus with Onclick not Working

    I'm new to AJAX, and honestly rather new to PHP, but I can't figure out why this isn't working. I'm trying to have two dropdown menus that pass variables to an sql query. The functions works when I only have state, but when I add the second dropdown menu for city, nothing happens.

    Any help would be appreciated.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function showUser()
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    state_var= document.getElementByID('state_select').value
    city_var = document.getElementByID('city_select').value
    var url="getresults2.php";
    url=url+"?state="+state_var+"&city="+city_var;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    </script>
    </head>
    <body>
    
    <?php
    print "<form>";
    print "<select name=\"state\" onchange=\"showUser()\" id=\"state_select\">";
    print "<option value=\"\">State:</option>";
    
     mysql_connect("db378422384.db.1and1.com", "dbo378422384", "cable1234") or die(mysql_error()); 
     mysql_select_db("db378422384") or die(mysql_error()); 
     $data_state = mysql_query("SELECT DISTINCT state, state2 from cupcake_shops order by state2")
     or die(mysql_error()); 
    
    while($info_state = mysql_fetch_array( $data_state )) 
     { 
     Print "<option value=\"".$info_state['state']."\">".$info_state['state']."</option>"; 
     }
    print  "</select>";
    print "</form>";
    
    print "<form>";
    print "<select name=\"city\" onchange=\"showUser()\" id=\"city_select\">";
    print "<option value=\"\">City:</option>";
    
     $data_city = mysql_query("SELECT DISTINCT city from cupcake_shops order by city")
     or die(mysql_error()); 
    
    while($info_city = mysql_fetch_array( $data_city )) 
     { 
     Print "<option value=\"".$info_city['city']."\">".$info_city['city']."</option>"; 
     }
    print  "</select>";
    print "</form>";
    
    ?>
    <br />
    <div id="txtHint"><b>Person info will be listed here.</b></div>
    
    </body>
    </html>

    PHP Code:
    <?php
    $state
    =$_GET["state"];
    $city=$_GET["city"];

     
    mysql_connect("db378422384.db.1and1.com""dbo378422384""cable1234") or die(mysql_error()); 
     
    mysql_select_db("db378422384") or die(mysql_error()); 

     
    $data mysql_query("SELECT * FROM cupcake_shops where state='$state' and city='$city'"
     or die(
    mysql_error()); 
     
    echo 
    "<table border='1'>
    <tr>
    <th>State</th>
    <th>City</th>
    <th>Name</th>
    <th>Address</th>
    <th>Phone</th>
    </tr>"
    ;

    while(
    $info mysql_fetch_array($data))
      {
      print 
    "<tr>";
      print 
    "<td><a href=\"".$info[website]."\"</a>" $info[name] . "</td>";
      print 
    "<td>" $info[address] . "</td>";
      print 
    "<td>" $info[city] . "</td>";
      print 
    "<td>" $info[state2] . "</td>";
      print 
    "<td>" $info[phone_number] . "</td>";
      print 
    "</tr>";
      }
    echo 
    "</table>";

    ?>

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    This would probably be better suited for our Ajax forum. I'll move it there...
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •