Jump to content

[SOLVED] returning a dropdown box value in my function


Luodeni

Recommended Posts

I created a function to dynamically create a list of date available which will later be checked with checkdate(). Now the funny thing is that i was able to echo the result back from the user but it just created a new <option> which isn't very neat. Therefore I'd like to ask whether anyone knows how to do this neatly, that means selecting the value from the already generated list. many thanks in advance :)

 

here is the code

 

<?php
function createMonths()
{
	for ( $iMonth = 1; $iMonth <= 12; $iMonth++ )
	{
		echo "<option>" . $iMonth . "</option>";
	}
	return $iMonth;
}

function validDateCheck( $dobMonth, $dobDay, $dobYear )
{
	if (checkdate( $dobMonth, $dobDay, $dobYear) == true )
	{
		$message .= "";
	}
	else
	{
		$message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " Is not a valid date, please correct it! <br />";
	}
	return $message;
}
?>

<select name="dateofbirthmonth" tabindex="7" class="dropdownStyle" style="width: 37px; ">
<?php
if ( $_POST['dateofbirthmonth'] != "")								{
	echo "<option value=" . $_POST['dateofbirthmonth'] . " selected>" . $_POST['dateofbirthmonth'] . "</option>";
}
else 
{
	echo "<option value='00' selected>M</option>";
}
?>
<?php createMonths(); ?>
</select>

I'd move this

if ( $_POST['dateofbirthmonth'] != "")                        {
      echo "<option value=" . $_POST['dateofbirthmonth'] . " selected>" . $_POST['dateofbirthmonth'] . "</option>";
   }
   else 
   {
      echo "<option value='00' selected>M</option>";
   }

To within your createMonths function

function createMonths()
{
    for ( $iMonth = 1; $iMonth <= 12; $iMonth++ )
    {
        $selected = (isset($_POST['dateofbirthmonth']) && $_POST['dateofbirthmonth'] == $iMonth) ? ' selected="selected"' : null ;
        echo "<option{$selected}>" . $iMonth . "</option>";
    }
    return $iMonth;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.