1

一般来说,我是 PHP、HTML 和 Web 开发的新手。

我试图让一个列表框从关联数组中读取条目,我设法做到了。The problem is that, I can't seem to access the correct value, when an option is selected. 无论选择哪个选项,它似乎总是产生相同的响应(“伦敦”)。我尝试用 ["listBox"] 替换 $city,但输出是 'selected="selected"',$city 似乎更接近我想要的,所以我将其改回。

我花了很多时间试图找出原因,如果有人可以帮助我,我将不胜感激,在此先感谢您。

代码如下:

<!DOCTYPE html lang="en"/>`    
<html>
    <head>
        <title>Array Section: ex5.php</title>
    <charset = "utf-8">
    </head>
    <body>
        <?php
        // Create an associate array 
        $countriesWithCities = array(
            "Japan" => "Tokyo",
            "Mexico" => "Mexico City",
            "USA" => "New York City",
            "India" => "Mumbai",
            "South Korea" => "Seoul",
            "China" => "Shanghai",
            "Nigeria" => "Lagos",
            "Brazil" => "Sao Paulo",
            "Egypt" => "Cairo",
            "England" => "London"
        );
        ?>
        <form action="ex5b.php" method="POST" />
        <h1>Ex5b.php </h1>
        <h3>"listBox">Please choose a country from the list box. </h3>
        <select name="listBox" id="listBox" size="9" >
            <?php foreach ($countriesWithCities as $individualCountry => $city) { ?>
                <option value= <?php $city; ?> selected="selected">
                    <?php echo $individualCountry;
                    >
                } ?></option>
        </select>

        <input type="submit" name="submitButton" id="submitButton" value="Submit form" />
        <form>
            <?php
            if (isset($_POST["submitButton"])) {
                echo "You chose " . $city;
            }
            ?>
    </body>
</html>
4

3 回答 3

1

您在 $city 之前缺少一个回声,在 $individualCountry 之后缺少一个额外的结束 >。同样,您</option>在 foreach 循环中的右大括号 (}) 之后也有结束标记。

<select name="listBox" id="listBox" size="9" > 
<?php 
    foreach($countriesWithCities as $individualCountry=>$city)
    {
?>
    <option value="<?php echo $city; ?>" selected="selected"> <?php echo $individualCountry; ?>
    </option> 
<?php } ?>
</select>
于 2013-03-28T23:31:35.787 回答
0

正确的代码

<!DOCTYPE html lang="en">
<?php
$countriesWithCities = array(
   "Japan" => "Tokyo",
   "Mexico" => "Mexico City",
   "USA" => "New York City",
   "India" => "Mumbai",
   "South Korea" => "Seoul",
   "China" => "Shanghai",
   "Nigeria" => "Lagos",
   "Brazil" => "Sao Paulo",
   "Egypt" => "Cairo",
   "England" => "London"
);
?>
<form action="ex5b.php" method="POST">
<h1>Ex5b.php </h1>
<h3>Please choose a country from the list box. </h3>
<select name="city" id="listBox">
   <?php foreach($countriesWithCities as $individualCountry=>$city){?>
   <option value=<?php echo $city; ?>> <?php echo $individualCountry; ?></option>
   <?php } ?>
</select>
   <input type="submit" name="submitButton" id="submitButton" value="Submit form" />
</form>

<?php if(isset($_POST["submitButton"]))
{
   echo "You chose " . $_POST['city'];
}
?>
</body>
</html>

作出的更正

  1. 您可能从一本书(PDF?)中复制了这段代码,因此在 doctype 之后开始 PHP 标记以及数组名称丢失。
  2. 表单缺少结束标记
  3. <select>缺少name属性
  4. 您在 PHP 代码中寻找$city而不是$_POST['city']

在我从你的问题中复制代码或者我只是复制了不完整的代码之后发生了很多事情:D

于 2013-03-28T23:37:32.077 回答
0
<option value= <?php $city; ?> selected="selected">

改成

<option value= <?php echo $city; ?> selected="selected">
于 2013-03-28T23:38:39.183 回答