下拉列表

时间:2017-09-30 13:09:30

标签: php html

当我选择除下拉列表中第一项之外的任何其他项目时,它会加载相应的值,然后再次从列表中选择第一项并且不保留我选择的项目。

enter image description here

在上面的图片中,我从下拉列表中选择了美国,它显示了相应的名称,然后再次在列表中看到印度(列表中的第一项)。 我想要的是在列表中应该看到名称USA(或我选择的任何名称)。 我是HTML的新手,请在下面的代码中建议一些更改,以实现上述功能。 我们是否有任何有助于实现上述目标的属性

<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;
    }
    }
    ?>

1 个答案:

答案 0 :(得分:0)

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

我重新安排了你的代码,应该是这样的。

所以select块中的PHP检查$_POST['n1']是否有值,如果有,它会打印第一个options标记,其值为$_POST['n1']并标记它被选中了。否则,它会打印第二个options标记。

  

这是一个更加先进的版本,可以解决您提到的那些问题

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