PHP: HTML: send HTML select option attribute in POST
Solution 1
<form name="add" method="post">
<p>Age:</p>
<select name="age">
<option value="1_sre">23</option>
<option value="2_sam">24</option>
<option value="5_john">25</option>
</select>
<input type="submit" name="submit"/>
</form>
You will have the selected value in $_POST['age']
, e.g. 1_sre
. Then you will be able to split the value and get the 'stud_name'
.
$stud = explode("_",$_POST['age']);
$stud_id = $stud[0];
$stud_name = $stud[1];
Solution 2
You can do this with JQuery
Simply:
<form name='add'>
Age: <select id="age" name='age'>
<option value='1' stud_name='sre'>23</option>
<option value='2' stud_name='sam'>24</option>
<option value='5' stud_name='john'>25</option>
</select>
<input type='hidden' id="name" name="name" value=""/>
<input type='submit' name='submit'/>
</form>
Add this code in Header section:
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
Now JQuery function
<script type="text/javascript" language="javascript">
$(function() {
$("#age").change(function(){
var studentNmae= $('option:selected', this).attr('stud_name');
$('#name').val(studentNmae);
});
});
</script>
you can use both values as
$name = $_POST['name'];
$value = $_POST['age'];
Solution 3
You will have to use JavaScript. The browser will only send the value
of the selected option (so its not PHP's fault).
What your JS should do is hook into the form's submit event and create a hidden field with the value of the selected option's stud_name
value. This hidden field will then get sent to the server.
That being said ... you shouldn't relay on the client to provide the correct data. You already know what stud_name
should be for a given value on the server (since you are outputting it). So just apply the same logic when you are processing the form.
Related videos on Youtube
![Sridhar](https://i.stack.imgur.com/rvzhk.jpg?s=256&g=1)
Sridhar
PHP developer, Android Developer, iOS Developer profile for Sridhar at Stack Overflow, Q&A for professional and enthusiast programmers http://stackoverflow.com/users/flair/1085737.png?theme=dark
Updated on January 21, 2021Comments
-
Sridhar over 3 years
I want to send the selected item value along with some attribute (stud_name) value. Is there any functionality in PHP to do so?
Here is the example one.
<form name="add"> Age: <select name="age"> <option value="1" stud_name="sre">23</option> <option value="2" stud_name="sam">24</option> <option value="5" stud_name="john">25</option> </select> <input type="submit" name="submit"> </form>
-
asprin over 11 yearsAFAIK, you'll have to do it using javascript/jquery. I don't think it is possible in PHP alone
-
seferov over 11 yearsI think the values are the IDs of the students. After getting IDs also you can student names (from database), right? Don't rely on the user side.
-
Sridhar over 11 years@asprin, If then , how to do in js
-
Jurgo over 11 yearsor you can add the stud_name to the value with an delimiter. And after post you can divide them on the delimiter.
-