如何发布选择列表中的所有选项? [英] How do I POST all options in a select list?

查看:143
本文介绍了如何发布选择列表中的所有选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择多个列表,其中包含一些项目.它是ACL的IP地址列表.人们可以添加/删除IP,然后保存列表.但是,除非您在列表中选择一个项目,否则$_POST[selectName]不包含任何值.我该怎么做?我知道我可以使用javascript做到这一点,但我宁愿坚持使用PHP.

I have a select multiple list that has a few items in it. It is a list of IP addresses for an ACL. People can add/remove IPs, and then save the list. However, unless you select an item on the list, $_POST[selectName] does not contain any values. How can I accomplish this? I know I can do this with javascript but I would rather stick to PHP.

推荐答案

编辑/更正:您需要JS.无法通过POST发送所有(选定和未选定)选项.您必须以编程方式选择所有选项,然后再提交.

Edit/corrected: You need JS. There is no way to send all (selected and not selected) options via POST. You have to programatically select all options before submission.

具有格式(file1.php)的文件:

File with form (file1.php):

<script type="text/javascript">
    function selectAll() 
    { 
        selectBox = document.getElementById("someId");

        for (var i = 0; i < selectBox.options.length; i++) 
        { 
             selectBox.options[i].selected = true; 
        } 
    }
</script>

<form method="post" action="file2.php">
    <select id="someId" name="selectName[]" multiple>
        <option value="123.123.123.123">123.123.123.123</option>
        <option value="234.234.234.234">234.234.234.234</option>
    </select>
    <input type="submit" name="submit" value=Submit onclick="selectAll();">
</form>

接收POST的文件(file2.php):

File that receives POST (file2.php):

<?php
    foreach ($_POST['selectName'] as $item)
    {
    print "$item<br/>";
    }
?>

这篇关于如何发布选择列表中的所有选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆