Jump to content

Problem with if concatenation


bugzy

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

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.