korbenmxli Posted August 31, 2012 Share Posted August 31, 2012 im having trouble with this code.... i have 2 tables oe with us states and other with countys in each state.. you maybe have the idea... the user check a state in one drop and the other filter the table to just of the countys in the selected state... my code works fine but... here?s my problem.... i had to put a "GO" button so the other drop do the filter and when i click the button the 2nd drop filter the table ok but the 1st table show the first element... heres the tables structures.... allstates: id, states countys: id, state, county and heres the code <body> <?php mysql_connect("localhost", "root", ""); mysql_select_db("ubication"); ?> <form action="" method="post"> <select name="state"> <?php $state_query = "SELECT id, states FROM allstates"; $state_result = mysql_query($state_query); while($state = mysql_fetch_array($state_result)) { if($_POST['sta'] == $state['id'] ) { echo '<option selected value="' . $state['id'] . '">' . $state['sta'] . '</option>'; } else { echo '<option value="' . $state['id'] . '">' . $state['sta'] . '</option>'; } } ?> </select> <input type="submit" name="submit" value="GO" /><br /><br /> <select name="city"> <?php if($_POST['submit']) { echo $_POST['state']; $city_query = "SELECT id, county FROM countys WHERE id = '{$_POST[state]}'"; $city_result = mysql_query($city_query); while($city = mysql_fetch_array($city_result)) { echo '<option value="' . $city['id'] . '">' . $city['county'] . '</option>'; } } ?> </select> </form> </body> </html> tnx in advance!!! Quote Link to comment https://forums.phpfreaks.com/topic/267840-stuck-with-chained-drop-downs/ Share on other sites More sharing options...
trq Posted August 31, 2012 Share Posted August 31, 2012 This type pf functionality is best suited to a client side language such as JavaScript rather than a server side language such as PHP. Quote Link to comment https://forums.phpfreaks.com/topic/267840-stuck-with-chained-drop-downs/#findComment-1374144 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2012 Share Posted September 1, 2012 the 1st table show the first element... The name of your <select is 'state', not 'sta' Quote Link to comment https://forums.phpfreaks.com/topic/267840-stuck-with-chained-drop-downs/#findComment-1374481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.