如何在循环HTML
选择中检索表单订阅后检索所选值?我设法用手动 if 语句解决了这个问题,但这不是动态的。 我还在cookie
中存储以前提交的值。我使用自我页面表单提交。
我设法从单选按钮中检索以前的值:
<input type="radio" name="spol" value="moški" checked="checked" <?php if (isset($_POST['spol']) && $_POST['spol'] == 'moški') {
echo ' checked="checked"';
} ?>>
但是在循环中找不到执行此操作的方法foreach
<!---Cookie "keks" is storing previous submited string-->
<select name="status">
<?php
$_COOKIE['keks'];
$statusi = ["Dijak", "Študent", "Zaposlen", "Brezposelni"];
$counter= 0;
foreach ($statusi as $status) {
$counter++;
if ($counter == 2) {
echo "<option value=" . $status . " selected>" . $status . "</option>";
} else {
echo "<option value=" . $status . ">" . $status . "</option>";
}
}
?>
</select>
如 html 注释中所述,$_COOKIE['keks'];
存储最后一个值。
您可能希望将该值存储到变量中或按原样使用它。 然后,将其与循环的当前迭代进行比较。
$lastValue = $_COOKIE['keks'];
// some code
foreach ($statusi as $status)
{
if ($status == $lastValue)
// mark the option as selected
else
// don't mark it
}