-1

我在这里得到了这段代码......

<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 变量来自已选择的内容。我想要做的是当用户选择一个选项时,该选项现在在下拉列表中被选中,并且在客户端点击提交后页面返回。

有人知道我在说什么吗?

谢谢

4

2 回答 2

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".

于 2012-05-28T21:34:27.420 回答
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>
于 2012-05-28T21:36:37.937 回答