du9537 2012-11-11 03:42
浏览 241
已采纳

如何从php中的HTML select元素中获取所有值?

I have a HTML form with two select elements(listboxes) and two buttons to move the data from one listbox to another(using JavaScript):

 <form action="page.php" method="post">
        <select name="select1" multiple="yes">
            <option value="1">Left1</option>
            <option value="2">Left2</option>
            <option value="3">Left3</option>
        </select>
        <input type="button" value="--&gt;" onclick="moveOptions(this.form.select1, this.form.select2);" /><br />
        <input type="button" value="&lt;--" onclick="moveOptions(this.form.select2, this.form.select1);" />
        <select name="select2" multiple="yes">
            <option value="4">Right1</option>
            <option value="5">Right2</option>
        </select>
        <input type="submit" name="submit" value="Submit">
</form>

When I hit the Submit button I want to get all the values stored in the listboxes.

foreach ($_POST['select1'] as $value)
    {
        //save data to database
    }

This just gets one selected value(if there was one).

I managed to get multiple values (if they are selected) by putting a [] after the name of the select.

<select name="select1[]" multiple="yes">

But this still doesn't gets the unselected values, and this way the data moving JavaScript function doesn't works either.

  • 写回答

3条回答 默认 最新

  • duanfei1975 2012-11-11 03:47
    关注
    var select1 = document.getElementById('select1');
    var values = new Array();
    
    for(var i=0; i < select1.options.length; i++){
        values.push(select1.options[i].value);
    }
    

    To pass the array from page to page, go here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题