html_options empty selected

From: Date: Tue, 30 Jul 2002 19:37:49 +0000
Subject: html_options empty selected
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5513@lists.php.net to get a copy of this message
I've run into a small bug with html_options (Actually, it's not a problem with smarty but in_array). It's not a big issue but thought I'd pass it along. Under these circumstances: 1) options array index starts at 0 2) the script passes back form element values to the form (say after validation and there is an error) 3) no option was selected for the above select box If no option was selected, then by default the variable $_POST['selectBoxName'] would be an empty string. When you assign to the selected value $_POST['selectBoxName'] this will match index 0 and mark it as selected. Sample: in optionsTest.php: <?php require_once ('Smarty.class.php'); $smarty=new smarty; $options=array(1=>'zero','one','two','three','four','five','six','seven'); $selected=$_POST['testSelect']; $smarty->assign('options',$options); $smarty->assign('selected',$selected); $smarty->display('optionsTest.tpl'); ?> in optionsTest.tpl: <form method="POST" action="optionsTest.php"> <select name="test" size=8> {html_options options=$options selected=$selected} </select> <input type="submit" name="submit"> </form> Now it would be a simple matter to check for an empty string and set it to null, but I felt there was a better way. I changed the in_array() check in Smarty.addons.php here: if (in_array($key, $selected, true)) $html_result .= " selected=\"selected\""; to use the third argument strict to force a type comparison. However, the problem didn't go away... and I can't figure out why. Any ideas? Rod Kreisler

« previous php.smarty.general (#5513) next »