我在这里得到了这段代码......
<form action="InvoiceNotice.php?action=invoicenotice" method="post">
<label for="fordays">Select Day</label>
<select name="daySelected" id="daySelected">
<option value="0">Today</option>
<?php
$array = array_combine(range(1,$InvoiceDaysArray['days']), range(1,$InvoiceDaysArray['days']));
foreach($array as $row => $value){
$selected = '';
$daySelected = 0;
if($daySelected == $row){
$selected = 'SELECTED';
}
echo "<option selected='" . $selected . "' value='" . $row . "'>" . $value . " days ago</option>";
}
?>
</select>
<input type="submit" name="button" id="button" value="Submit" />
</form>
我的问题是$selected
$ daysSelected变量来自已选择的内容。我想要做的是当用户选择一个选项时,现在在下拉列表中选择了该选项,并且在客户端点击提交后页面返回。
有人知道我在说什么吗?
由于
答案 0 :(得分:1)
我会做类似的事情:
foreach($array as $row => $value){
$selected = '';
if($_POST['daySelected'] == $row){
$selected = ' selected="selected"';
}
echo "<option" . $selected . " value='" . $row . "'>" . $value . " days ago</option>";
}
虽然您可能只需要selected
而不是selected="selected"
。
答案 1 :(得分:1)
我在你的代码中看到了一些问题:
首先你设置$daySelected = 0;
,然后尝试与数据库中的变量进行比较,第0天不在你的foreach循环中试试这个
<form action="InvoiceNotice.php?action=invoicenotice" method="post">
<label for="fordays">Select Day</label>
<select name="daySelected" id="daySelected">
<option value="0">Today</option>
<?php
$array = array_combine(range(1,$InvoiceDaysArray['days']), range(1,$InvoiceDaysArray['days']));
foreach($array as $row => $value){
$selected = '';
$daySelected = $_POST['daySelected'];
if($daySelected == $row){
$selected = "selected=SELECTED";
}else {$selected='';}
echo "<option '" . $selected . "' value='" . $row . "'>" . $value . " days ago</option>";
}
?>
</select>
<input type="submit" name="button" id="button" value="Submit" />
</form>