如何访问jQuery Mobile<;选择>;PHP中的元素


How do I access jQuery Mobile <select> elements in PHP?

我在jQuery Mobile应用程序的表单中有一个弹出的选择框。

<div data-role='fieldcontain'>
    <label for='fruits' class='select'>Favorite Fruits</label>
    <select name='fruits' id='fruits' multiple='multiple' data-native-menu='false'>
        <option>Favorite Fruits</option>
        <option value='1' checked='checked'>Apple</option>
        <option value='2' checked='checked'>Banana</option>
        <option value='3' checked='checked'>Cherry</option>
    </select>
</div>

选项元素的值属性等于水果的数据库ID。表单提交后,我尝试访问使用选择的元素数组

$fruits = $_POST['fruits'];

但是,var_dump($fruits)发出一个错误,指出$fruits没有定义。为什么?

经过一些修补,我认为您有三个问题:

  • 选择框名称应为name="fruits[]
  • checked="checked"应为selected="selected"
  • 空选项应为value=""