为什么 PHP 不能正常工作?

发布于 2024-10-21 09:04:04 字数 2346 浏览 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" />
<title>Loughborough University | Students Union</title>
<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));
print_r($day);


?>
<form action="test.php" method="post">
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>
<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" />
<title>Loughborough University | Students Union</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>

<?php   
$day = $_POST["day"];
$month = $_POST["month"];
$year = $_POST["year"];

echo $day;
echo $month;
echo $year;
?>

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

</body>
</html>

但是,举例来说,我选择第 1 天、第 1 个月和 2011 年,结果为 000。这是为什么,我可以采取什么措施来纠正这个问题?

感谢您的任何想法或建议。

I have 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" />
<title>Loughborough University | Students Union</title>
<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));
print_r($day);


?>
<form action="test.php" method="post">
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>
<input type='submit' value='send' name='send' />
</form>


</body>
</html>

and 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" />
<title>Loughborough University | Students Union</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>

<?php   
$day = $_POST["day"];
$month = $_POST["month"];
$year = $_POST["year"];

echo $day;
echo $month;
echo $year;
?>

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

</body>
</html>

However, say for example i choose, day 1, month 1, and year 2011 it comes out with 000. Why is this and what can i do to correct this?

Thanks for any ideas or suggestions.

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

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

发布评论

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

评论(6

执着的年纪 2024-10-28 09:04:04

在每个选项中,您都必须使用 $value 而不是 $key

<option value="<?php echo $value ?>" >

因为您传递给输入的只是键,而不是值。

例如:

$year = array(range(2011,2020));

创建一个数组:

array(
  0 => 2011,
  1 => 2012,
  2 => 2013,
  3 => 2014,
  4 => 2015,
  5 => 2016,
  6 => 2017,
  ...
)

因此,如果您选择 2014

<option value="<?php echo $key ?>" ><?php echo $value ?></option>

等于

<option value="3">2014</option>

在您发布的 test.php 页面中,您将获得 3 作为一年。

因此,只需按照我在开始时描述的方式进行更改即可解决您的问题。

In every option you have to use $value instead of $key:

<option value="<?php echo $value ?>" >

Because you pass to inputs just keys, not values.

For example:

$year = array(range(2011,2020));

creates an array:

array(
  0 => 2011,
  1 => 2012,
  2 => 2013,
  3 => 2014,
  4 => 2015,
  5 => 2016,
  6 => 2017,
  ...
)

so if you select 2014:

<option value="<?php echo $key ?>" ><?php echo $value ?></option>

is equals to

<option value="3">2014</option>

And in your posted test.php page you will get 3 as a year.

So simply change as I descriped on the beggining will solve your problem.

客…行舟 2024-10-28 09:04:04

您的问题可能出在这里:

$day = array(range(1,31));
$month = array(range(1,12));
$year = array(range(2011,2020));

由于 range() 已经创建了一个数组,因此您将一个数组包装在一个数组中。我怀疑这是故意的。

Your problem may be here:

$day = array(range(1,31));
$month = array(range(1,12));
$year = array(range(2011,2020));

Since range() already creates an array, you're wrapping an array inside an array. I doubt this is intended.

云巢 2024-10-28 09:04:04

尝试

$day = range(1,31);
...
<?php foreach($day as $value) { ?>
  <option value="<?php echo $value ?>" ><?php echo $value ?></option>

Try

$day = range(1,31);
...
<?php foreach($day as $value) { ?>
  <option value="<?php echo $value ?>" ><?php echo $value ?></option>
夜巴黎 2024-10-28 09:04:04

您编写了 value= 因此发送的实际值是数组的索引。由于第 1 天的索引为 0,第 1 个月的索引为 0,2011 年的索引为 0,因此您得到 000。

如果您使用 value=,结果将为 112011。

You wrote value=<?php echo $key ?> so the actual value which is sent is index to your array. Since day 1 has index 0, month 1 has index 0 and year 2011 has index 0 you got 000.

If you used value=<?php echo $value ?> the result would be 112011.

誰認得朕 2024-10-28 09:04:04

$day、$month 等应该分配给数组,而不是包含数组的数组(由 range 返回)。
所以:

$day = range(1,31);
$month = range(1,12);
$year = range(2011,2020);

另外,你的 foreach 应该以不同的方式编写:

foreach($day as $value) {
?>
<option value="<?php echo $value ?>" ><?php echo $value ?></option>
<?php
}

在你的代码中,你使用数组的条目索引作为值来回显选项标签(这就是为什么它打印 000 作为结果)。

希望这有帮助。

$day, $month etc should be assigned to arrays, not arrays containing arrays (returned by range).
So:

$day = range(1,31);
$month = range(1,12);
$year = range(2011,2020);

Also, your foreachs should be written differently:

foreach($day as $value) {
?>
<option value="<?php echo $value ?>" ><?php echo $value ?></option>
<?php
}

With your code you were echoing an option tag using the array's entry index as the value (that's why it was printing 000 as a result).

Hope this helps.

江城子 2024-10-28 09:04:04

需要进行一些更改:

  • 函数 range() 已经返回一个表。
  • foreach 语句中的第一个参数必须是数组,而不是增量。
  • 您正在浏览一个简单数组,而不是关联数组,这意味着键将是从 0array_length()-1 的整数。所以不需要使用钥匙。
  • 如果将 value 属性保留在 option 标记中,则该值将与显示的标签相同

尝试以下代码:(

$day = range(1,31);
<?php foreach($day as $value) { ?>
    <option><?php echo $value ?></option>
<?php }?>

对于月份和年份也是如此)

Several changes need to be made :

  • The function range() already returns a table.
  • The first argument in a foreach statement needs to be an array, not an increment.
  • You're browsing a simple array, not an associative one, meaning the keys will be integers going from 0 to array_length()-1. So there is no need to use the keys.
  • If you leave the value attribute in the option tag, the value will be the same as the displayed label

Try this code :

$day = range(1,31);
<?php foreach($day as $value) { ?>
    <option><?php echo $value ?></option>
<?php }?>

(Same goes for months and years)

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