multiple inputbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

multiple inputbox

Post by leewad »

Hi

I have a multiple inputbox with around 30 selections, what i need is when the form is submitted the same selections choose would keep highlighted.

here is the form:

Code: Select all

<select class="inputbox" name="district[]" MULTIPLE size=5>
<option>All Divisions</option>
<option>Bedfordshire & North Buckingham</option>
<option>Birmingham</option>
<option>British Universities</option>
<option>Combined Services</option>
<option>Cornwall & Dorset</option> 
<option>Derbyshire</option>
<option>Devon</option>
<option>East Lancashire</option> 
<option>Essex</option>
<option>Gloucester & Wiltshire</option>
<option>Hampshire & Isle of Wight</option> 
<option>Kent</option>
<option>Leicester, Rutland & Northampshire</option>
<option>Mid Anglia</option>
<option>Norfolk</option>
<option>North East</option>
<option>North West</option>
<option>Nottingham & Lincolnshire</option>
<option>Oxon & Berkshire & South Buckingham</option>
<option>Somerset & Bristol</option>
<option>South East</option>
<option>South West</option>
<option>Staffordshire</option>
<option>Suffolk</option>
<option>Surrey</option>
<option>Sussex</option>
<option>Tyne, Tees and Wear</option>
<option>Warwickshire</option>
<option>West Lancashire</option> 
<option>West Midlands</option>
<option>Yorkshire/Humberside</option>	
</select>
If I choose 3 of them and submitted the form the 3 selections would disapear, is there something I can do in php which will keep them selected?


Thanks
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

What I would do is use the following code for each <option> or simply populate the list from a database and use a loop with the following code within the loop:

Code: Select all

$district = $_REQUEST["district"];
foreach($district as $optionValue) {
  if($optionValue==$district)
        {
	     echo "selected=\"selected\"";
	}
}
This code basically uses a "foreach" loop to loop through the array "district[]" and then performs a conditional "if" statement to determine which <option> should be selected.

Also, see (this is for checkboxes, but principle applies): http://www.codewalkers.com/c/a/Miscella ... -in-PHP/3/
Post Reply