Jump to content

stuck with chained drop downs


korbenmxli

Recommended Posts

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!!!

Link to comment
https://forums.phpfreaks.com/topic/267840-stuck-with-chained-drop-downs/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.