Re: A better way to do this

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

 



On Sun, 2005-10-23 at 20:23 +0100, Ross wrote:
> I want the selct table to retain it's value on submit. The way I have done 
> it works but is a bit rubbish and was wondering if there is a more efficient 
> way. I just make the variable equal to "selected" when the form is submitted
> 
> <select name="table_name" id="table_name">
>             <option value="1" <?=$one; ?>>Please Select</option>
>             <option value="news" <?=$two; ?>News</option>
>             <option value="events" <?=$three; ?>Events</option>
>             <option value="publications" <?=$four; ?>Publications</option>
>         </select>

<?php
$selectValues = array(
	'1'		=> 'Please select',
	'news'		=> 'News',
	'events'	=> 'Events',
	'publications'	=> 'Publications'
);

print( '<select name="table_name" id="table_name">' );

foreach( $selectValues as $value => $option ) {

	print( '<option value="' . htmlspecialchars( $value ) . '"' );
	if( $_POST['table_name'] == $value ) {
		print( ' selected' );
	}
	print( '>' . htmlspecialchars( $option ) . '</option>' );

}

print( '</select>' );
?>

If you're using XHTML, replace ' selected' with ' selected="selected"'

-- 
Jasper Bryant-Greene
General Manager
Album Limited

e: jasper@xxxxxxxxxxx
w: http://www.album.co.nz/
p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303
a: PO Box 579, Christchurch 8015, New Zealand

-- 
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