0

This is my PHP code with smarty.

<p>
    <label for="dob">{$smarty.const.LBL_DATE_OF_BIRTH}</label>
    <span>
        <select class="left" id="bday" name="bday" style="width:60px;" >
            <option value="">- {$smarty.const.LBL_DAY} -</option>{$days}
        </select>
    </span>
</p>

and my PHP code is

for($bd=1;$bd<=31;$bd++)
{
    $bdkey=($bd<10)?"0".$bd:$bd;
    $selected='';
    if($bdkey==date("d",strtotime($data['dob']))) $selected ='selected';
    $days.="<option value=".$bdkey." $selected >".$bdkey."</option>";
}

$smarty->assign('days',$days); 

This value is inserted in DB. But its not selected the value. Like, if I select 15, and save, it inserted DB. But not showing selected value. Thanks in advance.

3
  • Try to use: $selected=' selected="selected" ;` Commented Mar 7, 2014 at 12:34
  • @Oscar Pérez No, this not work Commented Mar 7, 2014 at 12:36
  • 1
    Probably your if($bdkey==date("d",strtotime($data['dob']))) statement returns false. Print out each check to see, why don't you get the expected results. Commented Mar 7, 2014 at 12:40

1 Answer 1

3

Do it the Smarty way, Smarty has the excellent function html_options to create html select fields.

PHP:

$days = array("- ".LBL_DAY." -");
for($bd=1;$bd<=31;$bd++) {
    $days[] = ($bd<10)?"0".$bd:$bd;
}

$smarty->assign("days", $days);
$day = date("d",strtotime($data['dob']));
$smarty->assign("dob", $day);

Smarty:

<p>
    <label for="dob">{$smarty.const.LBL_DATE_OF_BIRTH}</label>
    <span>
        {html_options values=$days output=$days selected=$dob class="left" id="bday" name="bday" style="width:60px;"}
    </span>
</p>

This will use the array and the data from PHP and create the complete select element including all options, with the currently stored already selected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.