Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: HTML: send HTML select option attribute in POST

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>
like image 275
Sridhar Avatar asked Jan 29 '13 08:01

Sridhar


2 Answers

<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]; 
like image 83
rajmohan Avatar answered Sep 25 '22 02:09

rajmohan


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'];
like image 21
iJay Avatar answered Sep 24 '22 02:09

iJay