Reputation: 75
i have this code
<?php
$sexV = @$_POST['sex']; //the value of selected option 1 and 2
$sexL = @$_POST['sex']['label']; //the value of label of selected option male and female
if(@$_POST['doFun']){
echo $sexV;
}
?>
<form method="post">
<select name="sex" id="sex">
<option value="1" label="male">m</option>
<option value="2" label="female">f</option>
</select>
<input type="submit" name="doFun">
</form>
my question is ...how to get value of selected option and value of the label of the same option ?? thank you ^_^
Upvotes: 0
Views: 6249
Reputation: 1853
OPTION ONE: Doesn't require javascript but needs parsing PHP side
<?php
if(@$_POST['doFun']){
$sexVals = explode(',', $_POST['sex']);
$sexV = $sexVals[0]; //the value of selected option 1 and 2
$sexL = $sexVals[1]; //the value of label of selected option male and female
echo $sexV;
}
?>
<form method="post">
<select name="sex" id="sex">
<option value="1,male">m</option>
<option value="2,female">f</option>
</select>
<input type="submit" name="doFun">
</form>
OPTION TWO: Requires javascript which can be disabled by the user and all those nightmares from the clientside.
<form method="post">
<input type="hidden" name="sex-label" id="sex-label"/>
<select name="sex" id="sex">
<option value="1" label="male">m</option>
<option value="2" label="female">f</option>
</select>
<input type="submit" name="doFun">
</form>
<script>
var element = document.getElementById('sex');
var label = document.getElementById('sex-label');
element.onchange = function(){
label.value = element.options[element.selectedIndex].getAttribute("label");
};
</script>
Latter in PHP...
$sexL = $_POST['sex-label'];
Upvotes: 1