PHP Posting a combo box value
If I have a combobox like this:
<select name="gender">
<option value="M">Male</option>
<option value="F">Fema开发者_如何转开发le</option>
</select>
So when I post using:
$gender = $_POST['gender'];
I will get M or F as the result for $gender, how do I get both M, F and Male,Female?
As JamWaffles has said, there is no easy way to do this.
However, you do have a few options if you must:
Option 1 - Use jQuery to post the form for you. You could get the
val()
("M"/"F") of the combo box and also thehtml()
value ("Male"/"Female").Option 2 - Set the value to something like
value="M|Male"
and then useexplode()
to get each value separately ("M"/"Male").Option 3 - Just use a simple if/else statement on the posted page -
if($val == "M") $gender = "Male";
Male and Female are simply display values, the actual value that gets posted is either M or F. You can either use a simple
if $_POST['gender'] == "M" { $fullGender = "Male" } else { $fullGender = "Female" }
on the server side, or add a new hidden field to the form and update it when the gender select box changes, ie
$(function() {
$("[name=gender]").change(function() {
$("#fullGender").val($(["[name=gender] option:selected").text());
});
}
(and in the form)
<input type="hidden" name="fullGender" id="fullGender"/>
精彩评论