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
therat
Forum Commoner
Posts: 62 Joined: Wed Oct 01, 2003 2:44 pm
Location: London
Post
by therat » Mon Mar 21, 2005 12:55 pm
When validating a select list how can I default the list to what the user has chosen if other parts of the form have errors.
With a text field I have the following that shows what the user entered if other parts of the form have errors. I need to do the same thing for the select list
Text Box
Code: Select all
<input type="e;text"e; name="e;subs_title"e; size="e;32"e; value="e;<?php echo $subs_title; ?>"e; class="e;mainoption"e;>
Select List
Code: Select all
<select name="e;subs_res"e; class="e;mainoption"e;>
<option selected="e;selected"e;>Select....</option>
<option>640*480</option>
<option>800*600</option>
<option>1024*768</option>
<option>1152*864</option>
<option>1280*1024</option>
<option>1600*1200</option>
<option >Other</option></select>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Mon Mar 21, 2005 1:05 pm
I like to store my list options in an array and then array_unique with the post value in the list.
Code: Select all
$options = array($_POST['selected'],'640*480','800*600','1024*768','1152*864',',1280*1024','1600*1200','Other');
$options = array_unique($options);
echo '<select name="subs_res" class="mainoption">
<option selected="selected">Select....</option>';
foreach ($options as $option)
{
echo '<option>'.$option.'</option>';
}
echo '</select>';
therat
Forum Commoner
Posts: 62 Joined: Wed Oct 01, 2003 2:44 pm
Location: London
Post
by therat » Mon Mar 21, 2005 1:37 pm
Thanks, I'll try that.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Mar 21, 2005 1:41 pm
o.O
Code: Select all
$options = array('Select...' => 0, '640*480' => 1, '800*600' => 2, ... );
$out = '<select name="e;subs_res"e; class="e;mainoption"e;>' . "e;\n"e;;
$selection = (isset($_POSTї'subs_res']) ? intval($_POSTї'subs_res']) : 0);
foreach($options as $mark => $value)
{
$out .= '<option value="e;' . $value . '"e;' . ($select === $value ? ' selected="e;selected"e;' : '') . '>' . $mark . '</option>' . "e;\n"e;;
}
$out .= '</select>';
echo $out;