0

可能重复:
页面加载时已在下拉列表中选择了一个选项,并且还可以更改

我有一个下拉列表:

<select name="province">
                <option value="Alberta">Alberta</option>
                <option value="British Columba">British Columba</option>
                <option value="Manitoba">Manitoba</option>
                <option value="New Brunswick">New Brunswick</option>
                <option value="Newfoundland">Newfoundland</option>
                <option value="N.W. Territories">N.W. Territories</option>
                <option value="Nova Scotia">Nova Scotia</option>
                <option value="Ontario">Ontario</option>
                <option value="Prince Edward Island">Prince Edward Island</option>
                <option value="Saskatchewan">Saskatchewan</option>
                <option value="Yukon Territory">Yukon Territory</option>
                <option value="Quebec">Quebec</option>
</select>

该表单将选择提交到 MySQL 数据库。这是个人资料页面。当用户返回此配置文件页面以更改选择时,他的选择(从 MySQL 中获取)应显示为选中。任何想法如何实现这一目标?

IE。再次加载配置文件页面时,检查数据库,获取字段,将其与列表进行比较,然后选择该值作为选定值。

4

2 回答 2

1

你有正确的逻辑,去做吧!

  <?php
        $province_array = array('Alberta', 'British Columba', ......, 'Quebec');

        echo '<select name="province">';
        //get the user's setting with user_id from DB. You should get this before this script.
        $sql = "SELECT user_province FROM the_table WHERE user_id=$user_id;";
        $query = mysql_query($sql, $the_connection) or die (mysql_error());
        if($row = mysql_fetch_assoc($query)) {
          $user_province = $row['user_province']; 
           foreach($province_array as $value) {
         if ($value == $user_province) { //if the province==the user's setting, make it default 
           echo '<option value="'.$value.'" selected="selected">'.$value.'</option>';
         } else { //else, echo it as regular
           echo '<option value="'.$value.'">'.$value.'</option>';
         }
       }
    }
    echo '</select>';
    ?>
  1. 建立与数据库的连接。
  2. 确保您可以获得类似“user_id”的任何内容来区分不同的用户省与数据库。
  3. 然后是样本。
于 2012-09-28T20:46:01.833 回答
0

这是我要做的:

在显示表单之前初始化变量。

if ($_SERVER['REQUEST_METHOD'] == "POST") {  // if we're getting info from the form
    $defaultProvince = $_POST['province'];    // set the province to the selected pulldown
} else {
    $defaultProvince = "";                   // otherwise default to an empty string
}

$provinces = ("Alberta",                     // set up the provinces array
              "British Columbia",
              "Manitoba",
              "New Brunswick",
              ....
              );

然后在表单显示例程中我会添加:

echo '<select name="province">\n';
foreach ($provinces as $province) {         // run through our array of provinces
    echo '   <option value="$province"';
    if ($province === $defaultProvince) {   // if the default province matches this particular array province
                                            // then we'll select it
         echo ' selected="selected"'        // xhtml 1.0 Strict/1.1 compliant
    }
    echo '>$province</option>\n';
}
echo '</select>\n';
于 2012-09-28T17:57:52.683 回答