0

Im trying to get this simple page to do three drop down boxes, the boxes for date and year are fine, but month, the only drop down option you get is "$value" so i would assume that the array is at fault or the way it has been typed in by me.

<!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" xml:lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Calander</title>

</head>

<body>

<form action="calender.php" method="post">

<?php # Calendar.php

$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 
'September', 'October', 'November', 'December');

$days = range (1, 31);

$years = range (2008, 2018);

echo '<select name="month">';

foreach ($months as $key => $value) {

echo '<option value=\"$key\">$value </option>\n';

}

echo '</select>';

echo '<select name="day">';
foreach ($days as $value) {
 echo "<option value=\"$value\">
$value</option>\n";
}

echo '</select>';

echo '<select name="year">';
foreach ($years as $value) {
 echo "<option value=\"$value\">
$value</option>\n";
}

echo '</select>';

  
?>
</form>

</body>
</html>

hope someone can help.

Surf

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by edwinhermann
0

Hi surf and welcome to DaniWeb :)

echo '<option value=\"$key\">$value </option>\n';

This line is your problem. Swap all ' with " and vice versa. The problem is that $value is not evaluated inside single quotes. Your code should look like this:

echo "<option value='$key'>$value </option>\n";

Note that $key will be evaluated still because the single quotes are nested inside double quotes.

Edited by darkagn: Added greeting to new poster

0

Just a tip: You should be careful when outputting strings inside HTML. You should always use htmlspecialchars() to ensure that special HTML entities are properly encoded.

Consider using:

echo '<option value=\"'.htmlspecialchars($key).'\">'. htmlspecialchars($value).'</option>\n';

Edited by edwinhermann: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.