You are here: Re: [PHP] Re: setting initial vlaue of optionbox « PHP « IT news, forums, messages
Re: [PHP] Re: setting initial vlaue of optionbox

Posted by Jochem Maas on 09/28/39 11:20

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
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация