PHP:HTML:在 POST 中发送 HTML 选择选项属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14578559/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: HTML: send HTML select option attribute in POST
提问by Sridhar
I want to send the selected item value along with the some attribute (stud_name) value. Is there any functionality in PHP to do so? Here is the example one.
我想将选定的项目值与某些属性 (stud_name) 值一起发送。PHP 中是否有任何功能可以这样做?这是示例之一。
<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>
Thanks
谢谢
回答by rajmohan
<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'.
您将在 中拥有选定的值$_POST['age'],例如1_sre。然后,您将能够拆分该值并获得'stud_name'.
$stud = explode("_",$_POST['age']);
$stud_id = $stud[0];
$stud_name = $stud[1];
回答by iJay
You can do this with JQuery
你可以用 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:
在 Header 部分添加此代码:
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
Now JQuery function
现在 JQuery 函数
<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'];
回答by Jan Han?i?
You will have to use JavaScript. The browser will only send the valueof the selected option (so its not PHP's fault).
您将不得不使用 JavaScript。浏览器只会发送value所选选项的(所以它不是 PHP 的错)。
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_namevalue. This hidden field will then get sent to the server.
您的 JS 应该做的是挂钩表单的提交事件并使用所选选项的stud_name值创建一个隐藏字段。然后这个隐藏字段将被发送到服务器。
That being said ... you shouldn't relay on the client to provide the correct data. You already know what stud_nameshould 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.
话虽如此……您不应该中继客户端以提供正确的数据。您已经知道stud_name服务器上给定值应该是什么(因为您正在输出它)。因此,只需在处理表单时应用相同的逻辑即可。
回答by matpol
just combine the value and the stud_name e.g. 1_sre and split the value when get it into php. Javascript seems like hammer to crack a nut. N.B. this method assumes you can edit the the html. Here is what the html might look like:
只需将值和stud_name(例如1_sre)组合起来,并在将其放入php 时拆分该值。Javascript 看起来就像是敲碎坚果的锤子。注意此方法假定您可以编辑 html。这是 html 可能的样子:
<form name='add'>
Age: <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>
回答by Devang Rathod
You can use jquery function.
您可以使用 jquery 函数。
<form name='add'>
   <input type='text' name='stud_name' id="stud_name" value=""/>
   Age: <select name='age' id="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>
jquery code :
jQuery 代码:
<script type="text/javascript" src="jquery.js"></script>
<script>
    $(function() {
          $("#age").change(function(){
          var option = $('option:selected', this).attr('stud_name');
          $('#stud_name').val(option);
       });
    });
</script>

