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" conte开发者_StackOverflownt="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.
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.
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.
Try
$day = range(1,31);
...
<?php foreach($day as $value) { ?>
<option value="<?php echo $value ?>" ><?php echo $value ?></option>
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 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 foreach
s 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.
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
toarray_length()-1
. So there is no need to use the keys. - If you leave the
value
attribute in theoption
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)
精彩评论