简体   繁体   中英

PHP: How to change the values of a Select based on selections of another Select

I have a select with Countries and a Select with Cities and I want to display different values in the City Select depending on the selection of the Countries Select.

Something like: Value = countries.selection

SELECT city FROM cities WHERE Country = Value

$myrow[city]

See my Code Below (This bring values to the Selects from a Database and populated them.

There is probably a better way to code what I've code, I would also highly appreciate any suggestion to make it easier and more efficient.

Also, if it is better or easier to do it in Javascrip or jQuery I would accept suggestions in that direction as well.

Thank you so much

<html>
<body>


<select name="taskOption" id="drop_drop">
    <?php
    $db = pg_connect('host=localhost dbname=test user=myuser password=mypass');

    $query = "SELECT country FROM countries";

    $result = pg_query($query);
    if (!$result) {
        echo "Problem with query " . $query . "<br/>";
        echo pg_last_error();
        exit();
    }

    printf ("<option value=Select>Select a Country</option>");
    while($myrow = pg_fetch_assoc($result)) {

     printf ("<option value=$myrow[country]>$myrow[country]</option>");

}

    ?>

</select>


<select name="taskOption" id="drop_drop">
    <?php
    $db = pg_connect('host=localhost dbname=test user=myuser password=mypass');

    $query = "SELECT city FROM cities";

    $result = pg_query($query);
    if (!$result) {
        echo "Problem with query " . $query . "<br/>";
        echo pg_last_error();
        exit();
    }
    printf ("<option value=Select>Select a City</option>");
    while($myrow = pg_fetch_assoc($result)) {

     printf ("<option value=$myrow[city]>$myrow[city]</option>");

}

    ?>

</select>

</body>
</html> 

You have to make a request when the country changes, pass the value of the country to the server to find its cities.

     <!-- Note id change here -->
    <select name="taskOption" id="countries">
            <?php
            $db = pg_connect('host=localhost dbname=test user=myuser password=mypass');

            $query = "SELECT country FROM countries";

            $result = pg_query($query);
            if (!$result) {
                echo "Problem with query " . $query . "<br/>";
                echo pg_last_error();
                exit();
            }

            printf ("<option value=Select>Select a Country</option>");
            while($myrow = pg_fetch_assoc($result)) {

             printf ("<option value=$myrow[country]>$myrow[country]</option>");

        }

            ?>

        </select>

    <select name="taskOption" id="cities">
        <?php
        $db = pg_connect('host=localhost dbname=test user=myuser password=mypass');

        // Note query change here.
        $selectedCountry = $_GET['country'];

        $query = "SELECT city FROM cities where country_id = " + $selectedCountry;

        $result = pg_query($query);
        if (!$result) {
            echo "Problem with query " . $query . "<br/>";
            echo pg_last_error();
            exit();
        }
        printf ("<option value=Select>Select a City</option>");
        while($myrow = pg_fetch_assoc($result)) {

         printf ("<option value=$myrow[city]>$myrow[city]</option>");

    }

        ?>

    </select>

<script type='text/javascript'>
  // Note this new script.
  $(document).ready(function(){
     $('#countries').on('change', function(){
         window.location += '?country=' + $(this).val();
     });
  });
</script>

Please note the id attribute in select 's elements, and be careful of possible SQL injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM