0

我对带有选择选项的粘性表单有疑问。这是我的书所说的代码:

<?php
if(isset($_POST['submit']))
{
    echo ' thanks! ';
    $submit = $_POST['year'];
}
?>
<form action="" method="post">
<?php
$y = range(1920, 1950);
echo '<select name="year">';
for ($y = 2008; $y <= 2018; $y++) {
echo "<option value=\"$y\";
if ($year == $y) {
echo ' selected="selected"';
}
echo ">$y</option>\n";
}
echo '</select>';
?>
<input type="submit" name="submit" value="SUbmit"/>
</form>
4

3 回答 3

1

嘿,您在代码中犯了几个错误:您应该更改以下代码

$submit = $_POST['年份']; => $year = $_POST['year']; 而您忘记关闭这一行中的字符串: echo " echo "

工作代码是

<?php
if(isset($_POST['submit']))
{ 
   echo ' thanks! ';
   $year = $_POST['year'];
}
?>

<form action="" method="post">
<?php
   $y = range(1920, 1950);
   echo '<select name="year">';
   for ($y = 2008; $y <= 2018; $y++) {
     echo "<option value=\"$y\"";
     if ($year == $y) {
       echo ' selected="selected"';
     }
     echo ">$y</option>\n";
   }
   echo '</select>';
?>
<input type="submit" name="submit" value="SUbmit"/>
</form>
于 2012-03-25T03:05:16.993 回答
0

缺少表单的目标。看这里:

<form action="" method="POST">

“action”属性定义了表单数据将被处理的下一页。

编辑:粘性;你应该做这样的选择

for($i=1;$i<10;$i++){
  echo "<option value='".$i."' ".(($_POST["i"]==$i)?"selected":"").">".$i."</option>";
}
于 2012-03-25T02:33:47.197 回答
0

您忘记在第 13 行关闭字符串:还将年份设置为变量“$submit”,但随后检查变量“$year”。这两个应该具有相同的名称:

<?php
if(isset($_POST['submit']))
{
    echo ' thanks! ';
    $year = $_POST['year'];
}
?>
<form action="" method="post">
<?php
$y = range(1920, 1950);
echo '<select name="year">';
for ($y = 2008; $y <= 2018; $y++) {
echo "<option value=\"$y\"";
if ($year == $y) {
echo ' selected="selected"';
}
echo ">$y</option>\n";
}
echo '</select>';
?>
<input type="submit" name="submit" value="SUbmit"/>
</form>
于 2012-03-25T02:36:29.203 回答