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 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景