phpnewborn;10989043 wrote:
but is also creates a new option.
Actually it doesn't.
The first time the form is displayed, this line
<option value= "<?php echo $_POST['MonthX']; ?>" selected="selected"><?php echo $_POST['Monthx']; ?></option>
will be generating errors, Notice: Undefined index: MonthX which you would notice if you turned on error reporting in php.ini (error_reporting, log_errors and/or display_errors). The actual output of this statement, assuming you do not display errors, will be
<option value= "" selected="selected"></option>
Which you will see if you have another look at the select box the first time around (empty line), or if you use Firefox's plugin "Firebug", rclick the select box and "inspect element".
phpnewborn;10989043 wrote:
Is there a better way to do this?
Most definitely.
Number 1 on the to-do list is to set error_reporting to E_ALL. You will also need to specify a logfile for log_errors, and for non-production environments you should also turn on display_errors.
You should also always ascertain that user supplied data (where "user" doesn't have to be a person, but any external source of data) exist and is valid, see [man]isset[/man] and [man]empty[/man].
he ternary operator ?: (see [man]operators[/man], then comparison operators) is good for assigning default values.
The line of code below deals with the error you had (undefined index), since it does not attempt to access $POST['MonthX'] unless it is set.
It also deals with unsafe input (all external data is unsafe) by casting it to (int), which means that it will be 0 in case the originating string was not numeric (roughly true - see string conversion to numbers)
$monthX = isset($_POST['MonthX']) ? (int) $_POST['MonthX'] : 1;
And from this point on you will instead, safely, be using $monthX
The contents of your select box is a lot easier to create using php than writing the html code yourself directly, and also let's you easily deal with 'selected="selected"'. It also takes you away from the, in my opinion, bad habit of switching in and out of php all the time. Such code is harder to read since you have to keep looking for where php parsing starts and end every here and there in the middle of lines of html code.
This creates the month array for you. See [man]sprintf[/man] for details on [man]printf[/man] format specifiers. But in short, % starts a format specifier and should be followed by an additional argument. %d takes one of the following function arguments and treats it like a (signed) integer. Unless you specify which function argument to use, each new format specifier requiring an argument takes the next function argument, but with 1$, 2$ etc, you specify which of these arguments you want to use. %1$d and %1$s uses the same argument first as a signed integer, then as a string. And the last part, 02, found between $ and d is for padding and output length: pad with 0 to a string length of 2.
echo '<select name="monthX">';
for ($i = 1; $i < 13; ++$i)
{
printf('<option value="%1$d">%1$02d</option>', $i);
}
echo '</select>';
This still doesn't deal with month selection for you, but that is easily added.
printf('<option value="%1$d"%2$s>%1$02d</option>',
$i,
($i == $monthX ? ' selected="selected"': '')
);