View Full Version : i need help for php codes
Hi to all! Can somebody help me please with my project? I am developing a website my problem is that how would i retain the value of the <select> tags. For example:
<select name="gender">
<option> Select ender</option>
<option>Male</option>
<option>Female</option>
</select>
Once i will submit the form then i choose "Male" in the option and then some of my fields have an error entry, the "Male" option should retain the value. It will not go back with it's default value which is "Select gender". Please i need help. Thanks...a lot..
DragoNero
11-09-2007, 03:22 AM
<?php
$gender = $_POST['gender'] != '' ? $_POST['gender'] : ''; //Checks to see if gender is set or returns empty string if it isnt
?>
<select name="gender">
<option value=''>Select Gender</option>
<option value='male' <?php if($gender == 'male') echo "Selected"; ?>>Male</option>
<option value='female' <?php if($gender == 'female') echo "Selected"; ?>>Female</option>
</select>
greatstar00
11-09-2007, 03:23 PM
for select, in the tag <option>, there is a property called value, as dragonero showed
anything inside the value, and is selected, it will be sent to the server
for most of the html form tabs, and, watever in the values property, either typed, or selected, they will be sent to the server
Douglas
11-09-2007, 03:53 PM
For the Select Gender option, if you don't want anything to be sent for it you can use the nifty HTML tag <optgroup> which groups options. Here is an example using DragonNero's code:
<?php
$gender = $_POST['gender'] != '' ? $_POST['gender'] : ''; //Checks to see if gender is set or returns empty string if it isnt
?>
<select name="gender">
<optgroup label="Select Gender">
<option value="male"<?php if ($gender == 'male') echo ' selected="selected"'; ?>>Male</option>
<option value="female"<?php if ($gender == 'female') echo ' selected="selected"'; ?>>Female</option>
</optgroup>
</select>
But the way he posted will work just the same :)
Stimulus
11-09-2007, 03:54 PM
You guys lost me in the line that sets the $gender variable
Douglas
11-09-2007, 04:11 PM
It basically is a faster (not in speed but less typing) method of using if...else statements in PHP. Here is it's syntax:
$variable = (condition) ? 'value_if_true' : 'value_if_false';
Which could be used like this:
<?php
$a = 1;
$variable = ($a == 1) ? 'one' : 'not one';
echo '$a is ' . $variable;
?>
Which is the same as:
<?php
$a = 1;
if ($a == 1) {
$variable = 'one';
}
else {
$variable = 'not one';
}
echo '$a is ' . $variable;
?>
DragoNero
11-09-2007, 05:05 PM
@ douglas
you need to check your options attributes...
selected="selected" << is wrong
selected << is right
http://www.w3.org/TR/html4/interact/forms.html#adef-selected
Douglas
11-09-2007, 05:19 PM
Try it in your browser, if I could bet you money I would haha:
http://www.w3.org/TR/2002/REC-xhtml1-20020801/#h-4.5
It will work in any browser that keeps itself up to date in the last 5 years :)
DragoNero
11-09-2007, 05:45 PM
aside from the fact it isnt a minimised attribute :)
Go read through every HTML documentation file on W3 and you'll see im right
Douglas
11-09-2007, 07:23 PM
I didn't say yours was wrong, I just said mine was right as well as yours. Now saying it's not a minimized attribute is just plain stupid, because it is whether it's right or wrong. Yours is right in HTML 4.01, but wrong in XHTML 1.0 (which I used). It doesn't matter which way you do it, but mine is just as right as yours :)
DragoNero
11-10-2007, 04:24 AM
fair nuff lol
different headers tho :)
Stimulus
11-10-2007, 11:16 AM
Thanks guys, now I'll definitely use that,
but is there an elseif option?
Douglas
11-10-2007, 01:01 PM
No there is no elseif option, because you can only define one condition sorry :( Just use the normal way if you need the elseif option ;)
Stimulus
11-10-2007, 09:19 PM
KK
thanks a lot
hi to all! thank you so much for the responses. I'm so happy.. I tried the codes and it really works..thanks for all.. God bless all
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.