www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Passing multiple select options through POST

  1. #1
    Join Date
    Oct 2009
    Posts
    34

    resolved [RESOLVED] Passing multiple select options through POST

    I have got a select box set up and I'm trying to pass all the options through the POST method. I've spent some time trying to figure out how to do this online and was wondering if there is an easy way. I have it set to display all options on the following page (through a foreach loop). I want to avoid creating another PHP page in between the two page, but if necessary I will do that.

    I haven't been able to do this yet, I have it to select all options, but can't figure out how to send all options though.
    Last edited by iamanubcake; 09-15-2010 at 07:57 PM.

  2. #2
    Join Date
    Sep 2010
    Posts
    62
    Could you give a little more background on what you are trying to accomplish? Maybe walk us through the flow and the results you want so we can give you our best recommendation.

  3. #3
    Join Date
    Oct 2009
    Posts
    34
    I have a select element set up and I have it so that the user clicks on some options and when the click a button it adds those options to the select list (it starts off blank). I want to be able to pass ALL option elements in the select list when the Submit button is hit and display them on the next page.

  4. #4
    Join Date
    Sep 2010
    Posts
    62
    So you want to send the options via POST, even if they aren't selected? I think your best bet would be to use javascript to select the list items on form submission.

    Code:
    <select name="myList" multiple="multiple">
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
    </select>
    
    for(i = 0; i < document.forms[0].myList.length; i++)
    document.forms[0].myList[i].selected = true;
    Then every item in the list will be submitted to the server.

  5. #5
    Join Date
    Oct 2009
    Posts
    34
    This is what I already had. I have tested it otherwise and it does select all options. I have it set on the "onclick" event on the submit button.

    Code:
    function selectAll()
    {
    	for (var i = 0; i < document.getElementById("timeSlot").options.length; i++)
    	{
    		document.getElementById("timeSlot").options[i].selected = true;
    	}
    }
    Is there anything wrong with that code at all? How do I display it on the next page?

  6. #6
    Join Date
    Sep 2010
    Posts
    62
    Here is what I have and I think it will provide the results you want.

    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    function selectAll()
    {
    	for (var i = 0; i < document.getElementById("cars").options.length; i++)
    	{
    		document.getElementById("cars").options[i].selected = true;
    	}
    	return true;
    }
    </script>
    <form action="handle_event.php" onsubmit="return selectAll()" method="post">
    
    <select id="cars" name="cars[]" multiple="multiple" size="4">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
    </select>
    
    
    <input type="submit" name="submit" value="That's My Car" />
    </form>
    </body
    </html>
    and the php that I have just prints the array from the form values

    Code:
    <?php
    	print_r($_POST['cars']);
    ?>
    returns

    Code:
    Array ( [0] => volvo [1] => saab [2] => mercedes [3] => audi )

  7. #7
    Join Date
    Oct 2009
    Posts
    34
    Thank you very much. It works perfectly fine.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles