You are here: Re: [PHP] A better way to do this « PHP « IT news, forums, messages
Re: [PHP] A better way to do this

Posted by Jasper Bryant-Greene on 10/24/05 00:01

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@album.co.nz
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

 

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

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