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

发布于 2024-10-21 06:07:01 字数 2057 浏览 2 评论 0原文

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

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 页面,它也不会显示用户选择的内容。有什么帮助吗?

我哪里出错了?

I have two pages. One with the select boxes on and the send button. When the user chooses their options from the select boxes and clicks send it takes them to the second page which outputs their choices.

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>

Once the user makes their choices and clicks send it takes them to 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>

However, even though it goes to the test.php page it doesnt show what the user picked. Any help?

where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

芯好空 2024-10-28 06:07:01

您应该将 name 属性添加到

另外,您应该将
更改为
以获得 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>

You should add a name attribute to your <select> tags instead of your <option> tags.

Also, you should change <br> to <br /> for XHTML valid code.

Change you code to:

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>
脱离于你 2024-10-28 06:07:01

您的

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

It is your <select> elements which need the month/day/year name attributes, not their child options:

<select name="month">
  ...
</select>
月下凄凉 2024-10-28 06:07:01

您的 name 属性应位于 select 元素上,而不是其 option 上。

Your name attributes should be on the select elements, not their options.

陌若浮生 2024-10-28 06:07:01

您需要在

<select name="day">

You need to specify the name attribute on the <select> element, not the <option>, like this:

<select name="day">
云巢 2024-10-28 06:07:01

name 属性出现在select 上,而不是每个单独的option 上。

The name attribute goes on the select, not each individual option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文