基本上我正在尝试创建一个允许用户编辑表中数据的简单表单。它从数据库中提取数据并使用这些变量预填充表单。然后用户可以只编辑表单中的信息并重新提交数据。问题是......状态选择是一个函数,它自动创建填充所有状态的表单中的下拉框。我想如果我只是在该状态数组的开头添加一个变量,那么数组中的第一个选项将是已经存在于数据库中的状态,但它不起作用 - 它只是空白。 (下拉列表中显示的第一个选项为空白,状态列表的其余部分确实显示应该显示)
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$first = $row['first'];
$last = $row['last'];
$city = $row['city'];
$state = $row['state'];
$email = $row['email'];
$bday = $row['bday'];
function state_selection()
{
$str = '<select name="state2">';
$states = array(
"$state","Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"
);
foreach ($states as $state_name)
{
$str .= '<option value="'.$state_name.'">'.$state_name.'</option>';
}
$str .= '</select>';
return $str;
}
$editBlock = '<form method="post" action="./sadd.php">
<p>First Name: <input name="first2" value="' . $first . '" type="text" size=13 maxlength=25 /></p>
<p>Last name: <input name="last2" value="' . $last . '" type="text" size=13 maxlength=25 /></p>
<p>City:<input name="city2" value="' . $city . '" type="text" size=25 maxlength=50 /></p>
<p>State: '.state_selection().' </p>
<p>Email:<input name="email2" value="' . $email . '" type="text" size=50 maxlength=75 /></p>
<p>Birthday:<input name="bday2" value="' . $bday . '" type="text" size=10 maxlength=10 />(ex 1982-06-26)</p>
<p><input type="hidden" name="eb" value="ebf" /></p>
<p><input type="submit" name="submit" value="Update Contact" /></p>
</form>';
echo "$editBlock"; }
答案 0 :(得分:3)
$ state不在范围内,改为
function state_selection($state){
并打电话给
state_selection($state);
更可靠的方法,而不是预先添加
'<option value="'.$state_name.'" '.($state_name==$state)?'selected':'').'>'
答案 1 :(得分:1)
您无法在函数中访问变量$ state而不将其声明为全局变量。将此行添加到state_selection()
global $state;
另一种选择是传递$state
作为state_selection()
答案 2 :(得分:0)
这是一些更新的代码。一些关键的注意事项:
有任何问题让我知道。
function state_selection($states, $default) {
$str = '<select name="state2">';
foreach ($states as $state_name) {
$str .= '<option value="'.$state_name.'" ' . (($default == $state_name)?"selected":"") . '>'.$state_name.'</option>';
}
$str .= '</select>';
return $str;
}
$states = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois",
"Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada",
"New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
$row = mysql_fetch_assoc($result);
$editBlock = '<form method="post" action="./sadd.php">
<p>First Name: <input name="first2" value="' . $row['first'] . '" type="text" size=13 maxlength=25 /></p>
<p>Last name: <input name="last2" value="' . $row['last'] . '" type="text" size=13 maxlength=25 /></p>
<p>City:<input name="city2" value="' . $row['city'] . '" type="text" size=25 maxlength=50 /></p>
<p>State: ' . state_select($states, $row['state']) . ' </p>
<p>Email:<input name="email2" value="' . $row['email'] . '" type="text" size=50 maxlength=75 /></p>
<p>Birthday:<input name="bday2" value="' . $row['bday'] . '" type="text" size=10 maxlength=10 />(ex 1982-06-26)</p>
<p><input type="hidden" name="eb" value="ebf" /></p>
<p><input type="submit" name="submit" value="Update Contact" /></p>
</form>';
echo $editBlock;