Re: Re: setting initial vlaue of optionbox

From: Date: Mon, 04 Jul 2005 07:48:59 +0000
Subject: Re: Re: setting initial vlaue of optionbox
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-218073@lists.php.net to get a copy of this message
Jasper Bryant-Greene wrote:
Ross wrote:
heh Ross, hope you see this, my SMTP server kept saying you address was bogus for some reason and refused to send - so I only sent it to the list...
value="<?=$subject; ?>" Keeps the value of $subject that was previously entered in the textarea even after the form is submitted with php_self(). Is this possible with the listbox??
<select name="selectBox">
    <option value="someOption"<?php if($_GET['selectBox'] == 'someOption') { ?> selected<?php } ?>>Some Option</option>
</select>
a slightly shorter form: <select name="selectBox">
    <option value="someOption"<?= ($_GET['selectBox'] == 'someOption') ? 'selected' : ''; ?>>Some Option</option>
</select> but that still is not very satisfying - you have to write out that whole line for each option you create - better to incorporate the login into a loop that processes an associative array of 'options' so that you never (well not tomorrow anyway!) have to write it again...e.g: function makeOptions($name, $opts = array(), $selected = null) { $selTpl = '<select name="%s>%s</select>'; $optTpl = '<option value="%s" %s>%s</option>'; $output = array(); foreach($opts as $k => $v) { $s = $selected && ($k == $selected) ? 'selected="selected"' : ''; $output[] = sprintf($optTpl, $k, $s, $v); } return sprintf($selTpl, $name, join($output)); } ...untested, but hopefully it gives you some ideas.
Alter to suit your needs, of course. Jasper


« previous php.general (#218073) next »