Jump to content

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;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.