Re: Re: setting initial vlaue of optionbox

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux