我有一个带输入字段的表单。它工作得很好。当我在输入字段中写入内容并单击“提交”时,该值将存储到我的数据库中:
<form action="update.php?id=<?php echo $id?>" method="post">
<input name="option" type="text" value="<?php echo !empty($option)?$option:'';?>">
<button type="submit">Submit</button>
</form>
但我不需要输入字段,我需要一个选择框。我尝试了这个,但它不起作用:
<form action="update.php?id=<?php echo $id?>" method="post">
<select>
<?php
$default = "No";
$options = array("Yes","No");
foreach($options as $val) {
echo ($val == $default) ? "<option selected=\"selected\" value=\"$val\">$val</option>":"<option value=\"$val\">$val</option>";
}
?>
</select>
<button type="submit">Submit</button>
</form>
你知道我做错了什么吗?
答案 0 :(得分:1)
name
select
<select name="option">
<?php
$default = "No";
$options = array("Yes","No");
foreach($options as $val) {
echo ($val == $default) ? "<option selected=\"selected\" value=\"$val\">$val</option>":"<option value=\"$val\">$val</option>";
}
?>
</select>
答案 1 :(得分:0)
缺少name
输入的select
属性。
<select name="option">
您可以通过 -
访问它$_POST['option'] // for POST method
答案 2 :(得分:0)
// sample code if you need call the value from database here for static value
<form action="update.php?id=<?php echo $id?>" method="post" >
<select name="option">
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
</select>
<button type="submit">Submit</button>
</form>
答案 3 :(得分:0)
我终于找到了答案:
<select name="option">
<option <?php if ($option == Yes ) echo 'selected'; ?> value="Yes">Yes</option>
<option <?php if ($option == No ) echo 'selected'; ?> value="No">No</option>
</select>