optgroup get in php label associated

谁都会走 提交于 2019-12-11 05:16:42

问题


I have following code in a html form

<select name="category" class="input" onchange="ShowTB(this,'suggest');">
      <option value="0" selected="selected">
        [choose yours]
      </option>
      <optgroup label="Item">
      <option value="SubItem1"SubItem1</option>
    <option value="SubItem2">SubItem2</option>
  </optgroup>
      <option value="Item2">Item2</option>
      <optgroup label="Item3">
     <option value="SubItem4"SubItem4</option>
     <option value="SubItem5">SubItem5</option>
  </optgroup>
      <option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>

</select>

in php i get the value of field selected with:

$category = $_POST['category'];

in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3 How to ?


回答1:


Indeed, you only get the value. If you need more, just encode whatever you want into the value, for example:

<option value="Item3.SubItem5">SubItem5</option>

Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label.




回答2:


you could make the values arrays e.g.

<option value="Item3[SubItem5]">SubItem5</option> 

so then your $_POST['category'] should return an array



来源:https://stackoverflow.com/questions/8908658/optgroup-get-in-php-label-associated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!