Jump to content

Recommended Posts

Hey,

 

I am using the following code to loop 12 months backwards from the current month in a drop down list - using the get method.

 

I'm having difficulties trying to get the option selected once I have submitted the form.  For example, I am using this to filter months, so once a month has been selected and submitted, the first option in my drop down list is displayed and not the one that was selected and submitted. So basically, I need to be able to select a month > hit submit and when the form returns, have the drop down list show me the selected item. Hope that made sense.

 

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

	for($x=0;$x<12;$x++){

		$time = strtotime("$x months ago");
		$month = date('F Y',$time);
		$link = strtolower(date('M',$time));
		echo '<option value="'.$link.'">'.$month.'</month>';
	}

	echo '</select>';

?>

 

Does anyone have any ideas?

Link to comment
https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/
Share on other sites

I am not selecting the month from the database at this point in time. At the top of my page I am using a simple case switch that simply assigns the selected_month variable a value:

<?

switch ($_GET["month"]){

			case "jan":
				$selected_month = "January";
				break;
			case "feb":
				$selected_month = "February";
				break;

<!------------------------etc ---------------------------->

 

Any ideas?

 

 

Hello,

 

I tried successfully, the following slightly modified code to allow the submitted value to be selected on selectbox. Check if this could solve your issue. :)

<form method="get" action="select_prob.php">
<?php
	echo '<select name="month">';
	for($x=0;$x<12;$x++){

		$time = strtotime("$x months ago");
		$month = date('F Y',$time);
		$link = strtolower(date('M',$time));
		if(isset($_GET['month']) && $link == $_GET['month'])
		{
			echo '<option value="'.$link.'" selected="selected">'.$month.'</month>';
		}else{
		echo '<option value="'.$link.'">'.$month.'</month>';
		}
	}

	echo '</select>';

?>
	<input type="submit" value="Check">
</form>

 

Regards,

Sorry just one more thing. I can't seem to add: <option>value="?month=">All months</option> at the top of the drop down. When I add it above the loop it displays nothing.

 

Can this be done?

 

 

 

								<?

									echo 'Filter by month:';
									echo '<select name="month" style="margin:0 0 0 4px;" class="medium">';

									// loop months
									for($x=0;$x<12;$x++){

										$time = strtotime("$x months ago");
										$month = date('F Y',$time);
										$link = strtolower(date('M',$time));

										if(isset($_GET['month']) && $link == $_GET['month']) {

											echo '<option value="'.$link.'" selected="selected">'.$month.'</month>';

										} else {
										echo '<option value="'.$link.'">'.$month.'</month>';
										}
									}

									echo '</select>';
									echo '<input type="submit" value="Filter" class="submit" />';

							?>

Hello,

 

One of the following way to include option for 'All months' is ...

<?php
echo 'Filter by month:';
echo '<select name="month" style="margin:0 0 0 4px;" class="medium">';
echo '<option value="?month=">All months</option>';
// loop months
for($x=0;$x<12;$x++){

$time = strtotime("$x months ago");
$month = date('F Y',$time);
$link = strtolower(date('M',$time));

if(isset($_GET['month']) && $link == $_GET['month']) {

	echo '<option value="'.$link.'" selected="selected">'.$month.'</month>';

} else {
echo '<option value="'.$link.'">'.$month.'</month>';
}
}
echo '</select>';
echo '<input type="submit" value="Filter" class="submit" />';
?>

 

Hope, this might solve your issue.

Regards,

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.