HTML, CSS and anything else that deals with client side capabilities.
Moderator: General Moderators
edhen
Forum Newbie
Posts: 21 Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia
Post
by edhen » Sat Jul 17, 2010 9:59 pm
Hi, How would i got about a html form with a drop/selection box to have a default selection from users submitting. atm i got
Code: Select all
<select name="required[DOB_month]">
<option <? if ($_POST['required']['DOB_month'] == 1) { echo 'selected="selected"'; } ?>value="1">JAN</option>
<option <? if ($_POST['required']['DOB_month'] == 2) { echo 'selected="selected"'; } ?>value="2">FEB</option>
<option <? if ($_POST['required']['DOB_month'] == 3) { echo 'selected="selected"'; } ?>value="3">MAR</option>
<option <? if ($_POST['required']['DOB_month'] == 4) { echo 'selected="selected"'; } ?>value="4">APR</option>
<option <? if ($_POST['required']['DOB_month'] == 5) { echo 'selected="selected"'; } ?>value="5">MAY</option>
<option <? if ($_POST['required']['DOB_month'] == 6) { echo 'selected="selected"'; } ?>value="6">JUN</option>
<option <? if ($_POST['required']['DOB_month'] == 7) { echo 'selected="selected"'; } ?>value="7">JUL</option>
<option <? if ($_POST['required']['DOB_month'] == 8) { echo 'selected="selected"'; } ?>value="8">AUG</option>
<option <? if ($_POST['required']['DOB_month'] == 9) { echo 'selected="selected"'; } ?>value="9">SEP</option>
<option <? if ($_POST['required']['DOB_month'] == 10) { echo 'selected="selected"'; } ?>value="10">OCT</option>
<option <? if ($_POST['required']['DOB_month'] == 11) { echo 'selected="selected"'; } ?>value="11">NOV</option>
<option <? if ($_POST['required']['DOB_month'] == 12) { echo 'selected="selected"'; } ?>value="12">DEC</option>
</select>
and so on.... is there an easier more convenient way? thanks
kaszu
Forum Regular
Posts: 749 Joined: Wed Jul 19, 2006 7:29 am
Post
by kaszu » Sun Jul 18, 2010 4:54 am
Create an array with all values => labels and use foreach
Code: Select all
<select name="required[DOB_month]">
<?php
$DOB_month = array('1' => 'JAN', '2' => 'FEB', '3' => 'MAR', '4' => 'APR', '5' => 'MAY', ...); //List all months
$selected = '1';
if (!empty($_POST)) {
$selected = $_POST['required']['DOB_month'];
}
foreach($DOB_month as $key => $val) {
echo '<option ' . ($selected == $key ? 'selected="selected"' : '') . ' value="' . $key . '">' . $val . '</option>';
}
?>
</select>
edhen
Forum Newbie
Posts: 21 Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia
Post
by edhen » Sun Jul 18, 2010 6:30 am
ahhh thanks so much... so logical aswell... as a php starter i guess im gonna be doing it the hard way a bit till my head really cracks in gear with it all.... the ONLY thing i dont understand in the code is the ? mark next to the key and the : after selected... i havent seemed to come across those yet... other than that thanks again!
kaszu
Forum Regular
Posts: 749 Joined: Wed Jul 19, 2006 7:29 am
Post
by kaszu » Mon Jul 19, 2010 4:00 pm
I shouldn't have done that
That's shorthand version of if/else statement
Code: Select all
//Short
echo '<option ' . ($selected == $key ? 'selected="selected"' : '') . ' value="' . $key . '">' . $val . '</option>';
//Longer
echo '<option ';
if ($selected == $key) {
echo 'selected="selected"';
} else {
echo ' value="' . $key . '">';
}
echo $val . '</option>';
edhen
Forum Newbie
Posts: 21 Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia
Post
by edhen » Mon Jul 19, 2010 8:11 pm
yh lol i found that information out today... i found allot of useful stuff from a book i was reading this morning also to find alot of different ways to get the same result... again thanks