我有一个用户选择字段1的表单,然后选择字段2,该字段由他们在字段1上的选择填充。
问题是,在他们进入第2步之后,字段1没有显示他们的选择(即使选择仍然在隐藏的表单字段中进行。我不知道如何使用我设置下面的表单的方式我需要弄清楚如何用选中的选项标记它。
选择字段1和2的功能在
之下function GetCategoryList(){
$push .= "<form action=\"main.php\" method=POST>";
$push .= "<select name=cat>";
$result = mysql_query("SELECT * FROM `cats`") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$id = $row['id'];
$cat = $row['cat'];
$push .= "<option value=$id>$cat</option>";
}
$push .= "</select>";
$push .= "<input type=submit name=button id=button value=\"Set Category\"></form>";
return $push;
}
function GetSubCategoryList($cat){
$push .= "<form action=\"main.php\" method=POST>";
$push .= "<select name=subcat>";
$result = mysql_query("SELECT * FROM `subcats` WHERE cat = '$cat'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$id = $row['id'];
$subcat = $row['subcat'];
$cat = $row['cat'];
$push .= "<option value=$id>$subcat</option>";
}
$push .= "</select>";
$push .= "<input type=hidden name=cat value=$cat>";
$push .= "<input type=submit name=button id=button value=\"Set Sub-Category\"></form>";
return $push;
}
这是页面本身的代码
Category
<? echo GetCategoryList(); ?>
<br />
SubCategory
<? if(isset($_POST['cat'])){ echo GetSubCategoryList($_POST['cat']); } else { echo "<em>select a category</em>"; } ?>
<br />
答案 0 :(得分:1)
您需要使用selected
属性标记所选选项。
function GetCategoryList($selectedId=null) {
//...
$selected = ($id==$selectedId) ? "selected" : "";
$push .= "<option $selected value=$id>$cat</option>";
//...
}
echo GetCategoryList(isset($_POST['cat']) ? $_POST['cat'] : null);
(对于xhtml,它必须是selected="selected"
)