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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.