Geek Geek - 3 years ago 123
HTML Question

Selected item gets changed in dropdown list

When I select any other item apart from the first item in the dropdown list it loads the corresponding values and then again selects the first item from the list and does not retain the item I selected.

enter image description here

In the above image I had selected the USA from the dropdown list, it displayed the corresponding names and then again India (first item in the list) was seen in the list.
What I want is that the name USA (or whichever name I select) should be seen in the list.
I am new to HTML kindly advise some changes in below code to achieve above functionality.
Do we have any attribute which will help in to achieve the above goal

<html>
<body>

<form action = "StateNames.php" method = "post">

Select a Country Name
<select name = "n1">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="aus">Australia</option>
</select><br><br>
<input type="submit" value="Search" name = "Submit">

</form>

</body>
</html>

<?php

if(isset($_POST['n1']))
{
$name = $_POST['n1'];

$India = array("Maharashta", "Goa", "Himachal Pradesh", "Jammu and
Kashmir", "Uttaakhand");
$USA = array("New York", "California", "Washington", "Texas", "Utah");
$Australia = array("New South Wales", "Victoria", "Queensland",
"Tasmania");

switch($name)
{
case "india":
foreach($India as $i)
echo $i ."<br/>";
break;

case "usa":
foreach($USA as $u)
echo $u ."<br/>";
break;

case "aus":
foreach($Australia as $a)
echo "$a <br/>";
break;
}
}
?>

Answer Source

<html>
<body>

<form action = "StateNames.php" method = "post">

    Select a Country Name 
    <select name = "n1">
    <?php if(isset($_POST['n1'])) { ?>
     <option value="<?php echo $_POST['n1']; ?>" selected><?php echo $_POST['n1']; ?></option>
    <?php } else { ?>
      <option value="">Choose a option</option>
    <?php } ?>
      <option value="india">India</option>
      <option value="usa">USA</option>
      <option value="aus">Australia</option>
    </select><br><br>
    <input type="submit" value="Search" name = "Submit">  

</form>

<?php

if(isset($_POST['n1']))
{
    $name = $_POST['n1'];

    $India = array("Maharashta", "Goa", "Himachal Pradesh", "Jammu and 
Kashmir", "Uttaakhand");
    $USA = array("New York", "California", "Washington", "Texas", "Utah");
    $Australia = array("New South Wales", "Victoria", "Queensland", 
"Tasmania");

    switch($name)
    {
        case "india":
            foreach($India as $i)
                echo $i ."<br/>";
                break;

        case "usa":
            foreach($USA as $u)
                echo $u ."<br/>";
                break;

        case "aus":
            foreach($Australia as $a)
                echo "$a <br/>";
                break;
    }
    }
    ?>

</body>
</html> 

I have rearranged your code and it should be something like that.

So the PHP in the select block, checks is the $_POST['n1'] has a value, if so it prints the first options tag with the value of $_POST['n1'] and mark it as selected. Otherwise, it prints the second options tag instead.

Here is a much more advance version that solve those problems you've mentioned

<?php

// This creates a multidimestional array of countries
// Syntax Key => Value
// Country => States
$countries = array(
    "India" => array("Maharashta", "Goa", "Himachal Pradesh", "Jammu and Kashmir", "Uttaakhand"),
    "USA" => array("New York", "California", "Washington", "Texas", "Utah"),
    "Australia" => array("New South Wales", "Victoria", "Queensland", "Tasmania")
);
?>
<html>
    <body>
        <form action = "StateNames.php" method = "post">
            Select a Country Name 
            <select name = "n1">

            <!-- If a value already selected, show that -->
                <?php if (isset($_POST['n1'])) { ?>
                    <option value="<?php echo $_POST['n1']; ?>" selected><?php echo $_POST['n1']; ?></option>
                <?php } else { ?>
                    <option value="" selected>Choose a option</option>
                <?php } ?>

                <!-- Foreach loop that map Key as $country and Value as $states  -->
                <?php foreach($countries as $country => $states) { ?>

                <!-- If n1 already set -->
                    <?php if (isset($_POST['n1'])) {?>

                    <!-- Skip that option to avoind duplication on the list. In this case $country is not equal to n1, add as option -->
                        <?php if ($_POST['n1'] != $country) { ?>
                            <option value="<?php echo $country; ?>"><?php echo $country; ?></option>
                        <?php } ?>

                        <!-- If n1 not set print all options -->
                    <?php } else { ?>
                        <option value="<?php echo $country; ?>"><?php echo $country; ?></option>
                    <?php } ?>
                <?php } ?>
            </select>
            <br>
            <br>
            <input type="submit" value="Search" name = "Submit">
        </form>
        <?php
if(isset($_POST['n1']))
{
    $name = $_POST['n1'];

    // Here is the n1 is set we loop through array and print all the states out for that selected country
    foreach($countries as $country => $states) {
        if ($name == $country) {
            foreach($states as $state) {
              echo $state ."<br/>";  
            }
        }
    }
}
        ?>
    </body>
</html>

Live example, only good for 48H

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download