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.
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>";
?>
Comment