为什么 PHP 不能正常工作?
我有我有两页。一个带有选择框和发送按钮的。当用户从选择框中选择他们的选项并单击“发送”时,会将他们带到输出他们的选择的第二页。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在每个选项中,您都必须使用
$value
而不是$key
:因为您传递给输入的只是键,而不是值。
例如:
创建一个数组:
因此,如果您选择
2014
:等于
在您发布的
test.php
页面中,您将获得3
作为一年。因此,只需按照我在开始时描述的方式进行更改即可解决您的问题。
In every option you have to use
$value
instead of$key
:Because you pass to inputs just keys, not values.
For example:
creates an array:
so if you select
2014
:is equals to
And in your posted
test.php
page you will get3
as a year.So simply change as I descriped on the beggining will solve your problem.
您的问题可能出在这里:
由于
range()
已经创建了一个数组,因此您将一个数组包装在一个数组中。我怀疑这是故意的。Your problem may be here:
Since
range()
already creates an array, you're wrapping an array inside an array. I doubt this is intended.尝试
Try
您编写了
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.$day、$month 等应该分配给数组,而不是包含数组的数组(由
range
返回)。所以:
另外,你的 foreach 应该以不同的方式编写:
在你的代码中,你使用数组的条目索引作为值来回显选项标签(这就是为什么它打印 000 作为结果)。
希望这有帮助。
$day, $month etc should be assigned to arrays, not arrays containing arrays (returned by
range
).So:
Also, your
foreach
s should be written differently: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.
需要进行一些更改:
range()
已经返回一个表。0
到array_length()-1
的整数。所以不需要使用钥匙。value
属性保留在option
标记中,则该值将与显示的标签相同尝试以下代码:(
对于月份和年份也是如此)
Several changes need to be made :
range()
already returns a table.foreach
statement needs to be an array, not an increment.0
toarray_length()-1
. So there is no need to use the keys.value
attribute in theoption
tag, the value will be the same as the displayed labelTry this code :
(Same goes for months and years)