Gossamer Forum
Home : General : Perl Programming :

Select Box, item from database selected

Quote Reply
Select Box, item from database selected
Hi All,

I want to create a select box in a customer form which lists all the countries and saves the selected country to their database.

When they log back in and view their profile the select box will automatically select their country.

here is a snip of the select box code.

Code:
<select NAME="Country">
<option VALUE="" selected>Select</option>
<option VALUE="AF">Afghanistan </option>
<option VALUE="AL">Albania </option>
<option VALUE="DZ">Algeria </option>
<option VALUE="AS">American Samoa </option>
<option VALUE="AD">Andorra </option>
<option VALUE="AO">Angola </option>
<option VALUE="AI">Anguilla </option>
<option VALUE="AQ">Antarctica </option>
<option VALUE="AG">Antigua And Barbuda </option>
<option VALUE="AR">Argentina </option>
<option VALUE="AM">Armenia </option>
<option VALUE="AW">Aruba </option>
<option VALUE="AU">Australia </option>
<option VALUE="AT">Austria </option>
<option VALUE="AZ">Azerbaijan </option>


and here is the current code for that section of the form

Code:
<tr>
<td><font face=$normal_font_style size=$normal_font_size><b>Country</b></font></td>
<td><input type="text" name="country" value="$queryresult[8]"></td>
</tr>

Thanks in advance
Regards

Mark
http://www.host4.me.uk

No doubt i'll be back for more answers!

Last edited by:

mdj1: Aug 16, 2003, 7:48 PM
Quote Reply
Re: [mdj1] Select Box, item from database selected In reply to
Try something like;

Code:
my $val = "UK";

my @fields = qw('UK','Spain','France','Germany');

$select = " <select NAME=\"Country\"> ";

foreach (@fields) {

if ($_ eq $val) {
$select .= "<option VALUE="$_" selected>$_</option>";
} else {
$select .= "<option VALUE="$_" selected>$_</option>";
}

}

$select .= "</select>";

Set $val so that it grabs the database value, and then work from there. Thisis untested, but it should give you an idea on how to do it :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!