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

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.

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';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.