最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How do I POST all options in a select list? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Mar 4, 2013 at 9:25 koopajah 25.6k9 gold badges82 silver badges108 bronze badges asked Mar 3, 2013 at 20:28 SteveSteve 3,0465 gold badges30 silver badges39 bronze badges 3
  • 4 The browser doesn't send those values to PHP, you'll need Javascript to manipulate the submission process. Hence, there is no PHP only way to do this – Pekka Commented Mar 3, 2013 at 20:30
  • Have you seen this? stackoverflow.com/questions/2407284/… – Kamil Commented Mar 3, 2013 at 20:31
  • 2 The traditional way to do this is use making the select multi-selectable, then on form submission have javascript select all options. – Jayy Commented Mar 3, 2013 at 20:38
Add a comment  | 

2 Answers 2

Reset to default 18

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.

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>

File that receives POST (file2.php):

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

Just to tack on this you could also use the jQuery version of @Kamil's code which is a little simpler than the loop:

<script type="text/javascript">
jQuery('[name="form1"]').on("submit",selectAll);

function selectAll() 
{ 
    jQuery('[name="selectName[]"] option').prop('selected', true);
}

</script>
<form name="form1" 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>
发布评论

评论列表(0)

  1. 暂无评论