• Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us
Blue Orange Green Pink Purple

POSTing unselected OPTIONS in a SELECT tag

Posted in CSS & XHTML, Javascript & Libraries, PHP/MySQL. on Monday, February 15th, 2010 by Aneeska
Feb 15

When a form is submitted to the server, only selected values in a multiple SELECT box reaches the server. It is not possible to access the unselected options in a SELECT box from the server. Though this is not really required always, there are situations where you would want to access all the options in a SELECT tag. One example is, when you allow users to select and move options from one SELECT box to another and the second SELECT box represents the user selection.

The below function can be called before a form is submitted and it will read all the options in a SELECT box and will append to the form as a multiple selection INPUT field. Thus, the server will be able to read the value as an array or options.

This example also gives you an idea how an INPUT array form element is created using Javascript.

The form could look like this.

<form action="move_select.html" method="get" onsubmit="return postSelect(this);">
  <select name="multipleValues[]" id="multipleValues" size="5" multiple>
    <option value="1">America</option>
    <option value="2">Canada</option>
    <option value="3">China</option>
    <option value="4">England</option>
  </select>
  <input name="Submit" type="submit" value="Submit" />
</form>

Here is the Javascript which will be called before the form is submitted. The id of the Multiple SELECT box is “multipleValues”. The script loops through the select box and appends it as an input array element to the FORM and submit the form to the server.

<script>
function postSelect(daForm) {
	var postAll;
	var sB = document.getElementById("multipleValues");
	for(var k=0; k<sB.length; k++){
		postAll = document.createElement("input");
		postAll.setAttribute("type", "hidden");
		postAll.setAttribute("name", "selectBoxData[]");
		postAll.setAttribute("value", sB[k].value);
		daForm.appendChild(postAll) ;
	}
	return false;
}
</script>

Now in your server script, you can access the options (unselected) in the SELECT box as an array.

The php code for accessing the above SELECT box values would be this. Here, you can access “selectBoxData” as an array.

print_r($_POST['selectBoxData']);

ASK a Question!

Bookmark and Share

Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image
  • Search a Topic


  • Got a Question?
      ASK and get a Solution! ASK
  • Categories
    • CakePHP
    • Computers
    • CSS & XHTML
    • Enterprise Web
    • Flash & Action Script
    • Flex
    • Javascript & Libraries
    • Perl Programming
    • PHP/MySQL
    • Programming Techniques
    • Regular Expression
    • Social Media
  • Recent Visitors
  • Recent Articles
    • How to Protect your SWF (AS2 or AS3). Prevent SWF Decompile using SWF Protector
    • PHP – Search in an Array for Values Matching a Pattern – Regex, Wildcard
    • PHP – How to Get Browser Properties and Capabilities – get_browser()
    • CakePHP – Search for records between two dates inclusively
    • Notebook/Laptop hangs frequently/random (on Low Battery when AC power is connected)
    • Swap Values Without Temp or A Third Variable in PHP
    • CakePHP – Auth Login Redirect Problem with Plugins
    • Convert PHP array to Javascript array
    • CakePHP – Accessing a model in AppController or in any Controllers
    • Save Images from Flash – Actionscript 3, Filereference.save, JPGEncoder
  • Archives
    • September 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
  • Tags
      vertically center horizontally align dead center div amoeba ask credit card workbench decryption solutions kiosk questions horizontal center align vertical center align css trick solution css encryption as2 flash regex as3 Regular Expression action script mysql array CakePHP php

  • Donate

      If you find this site helpful, please donate to Amoeba Solution Kiosk. We will be using the money received for conducting technology awareness camps and seminars for school and college students in rural areas of Kerala, India.

  • Links
    • Amoeba Solutions
    • Blog Catalog
  • Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us

© Copyright Amoeba Solution Kiosk. All rights reserved.
An Amoeba Concept!

Back to Top