无法使用PHP从一个页面获取信息

时间:2011-03-10 13:55:36

标签: php html select post

我有两页。一个选择框和发送按钮。当用户从选择框中选择他们的选项并点击发送时,它会将他们带到输出他们选择的第二页。

date_change.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>
<?php
$day = array(range(1,31));
$month = array(range(1,12));
$year = array(range(2011,2020));
?>
<form action="test.php" method="post">
Day:
<select >
  <?php foreach($day[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>" name="day"><?php echo $value ?></option>
  <?php }?>
</select>
<br>
Month:
<select>
  <?php foreach($month[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>" name="month"><?php echo $value ?></option>
  <?php }?>
</select>
<br>
Year:
<select>
  <?php foreach($year[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>" name="year"><?php echo $value ?></option>
  <?php }?>
</select>
<input type='submit' value='send' name='send' />
</form>


</body>
</html>

一旦用户做出选择并点击发送,就会将他们带到test.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>

Date Selected: <?php echo $_POST["day"];echo $_POST["month"];echo $_POST["year"]; ?>

</body>
</html>

然而,即使它进入test.php页面,它也不会显示用户选择的内容。有什么帮助吗?

我哪里错了?

5 个答案:

答案 0 :(得分:2)

您应该在name代码中添加<select>属性,而不是<option>代码。

此外,您应该将<br>更改为<br />以获取XHTML有效代码。

将您的代码更改为:

Day:
<select name="day">
  <?php foreach($day[0]++ as $key => $value) { ?>
    <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
  <?php } ?>
</select>
<br />
Month:
<select name="month">
  <?php foreach($month[0]++ as $key => $value) { ?>
    <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
  <?php } ?>
</select>
<br />
Year:
<select name="year">
  <?php foreach($year[0]++ as $key => $value) { ?>
    <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
  <?php } ?>
</select>

答案 1 :(得分:2)

您的<select>元素需要月/日/年名称属性,而不是他们的子选项:

<select name="month">
  ...
</select>

答案 2 :(得分:2)

您的name属性应该在select个元素上,而不是option个。

答案 3 :(得分:2)

您需要在name元素上指定<select>属性,而不是<option>,如下所示:

<select name="day">

答案 4 :(得分:2)

name属性取决于select,而不是每个人option