Jump to content

Recommended Posts

Hello guys..

 

I have this function

 


<?php
function me_date_dropdown($year_limit = 0)
{

  $html_output = '           <select name="date_year" id="year_select">'."\n";


			for ($year = 1940; $year <= (date("Y") - $year_limit); $year++)
			{

						  $html_output .= "               <option selected=";
						  
						  if(isset($_POST['submit']))
						  {
							 if($_POST['date_year'] == $year)
							  {
							 	  $html_output .= "\"selected\"";
							  }
						  }

						  $html_output .= ">". $year . '</option>'."\n";

            }
        $html_output .= '           </select>'."\n";

return $html_output;


}

?>

 

 

If the user wasn't able to pass the validation on the form registration, I want to pass the previous year he set during the submission.

 

Problem is, for example I chose year "1985", it's always passing me the latest year which is "2012". I don't know if there's a problem on the concatenation or on the if station..

 

Anyone?

Link to comment
https://forums.phpfreaks.com/topic/266850-problem-with-if-concatenation/
Share on other sites

change this:

$html_output .= "<option selected=";
						  
if(isset($_POST['submit']))
{
if($_POST['date_year'] == $year)
{
	  $html_output .= "\"selected\"";
}
}

$html_output .= ">". $year . '</option>'."\n";

 

for this:

$html_output .= "<option ";

if(isset($_POST['submit']))
{
if($_POST['date_year'] == $year)
{
	  $html_output .= "selected=\"selected\"";
}
}

$html_output .= ">". $year . '</option>'."\n";

 

Just print "selected=" if you want the option active. You are printing "selected" on all of them.

 

And you can write this way too:

$selected = '';
if(isset($_POST['submit']) && ($_POST['date_year'] == $year))
$selected = 'selected="selected"';

$html_output .= "<option $selected>$year</option>\n";

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.