Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submit entire SELECT list, not only selected items 1

Status
Not open for further replies.

cmayo

MIS
Joined
Apr 23, 2001
Messages
159
Location
US
Seems like I used to know how to do this, but I'm getting old and my brain only works when it wants to these days.

I'm allowing the user to create a SELECT list by adding items to it and reordering them, and need to be able to submit the entire list, in order, instead of only sending the list items the user might have selected.

Seems like I used to use Javascript to iterate through the list and add each item to a hidden field, then submit the hidden field along with the form, but that seems pretty clunky in retrospect.

I'm using PHP for server-parsed code, might there be a clean way to do this with PHP, like maybe name the SELECT like a PHP array (<select name="listname[]">) and let PHP deal with the list as an array?

Any suggestions would be apprciated.
 
Seems like I used to know how to do this, but I'm getting old and my brain only works when it wants to these days.

You used to do a lot of web work using frames, eh?


[small]Ignore me, that's an inside joke[/small]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Well... not a lot, no. Frames sucked, even back then.
 
You can grab the select box object and loop through it to get all the values and store them into a hidden form field or you could grab the innerHTML of the select box and pass that. Then parse out the values however you like in PHP.

Here is an example of pulling the fields separated by commas and storing them in a form field.

In another script I wrote I pulled the entire form innerHTML, modified all fields to lock them so they cannot be edited and passed the entire HTML string in a form field so that my processing page could send that out as the submitted email version of the form. That way I do not have to re-create the form for email purposes.

Code:
<html>
<head>
<script type="text/javascript">
function checksel() {
  var sel = document.getElementById('myselect').options;
  var selvals='';
  for (var x=0;x<sel.length;x++) {
    if (selvals != '')
      selvals+=',';
    selvals+=sel[x].value;
  }   
  document.getElementById('mysel').value = selvals;
}
</script>
</head>
<body onload="checksel();">
<form>
<select id="myselect" size="1">
<option value="Uno">Uno</option>
<option value="Dos">Dos</option>
<option value="Tres">Tres</option>
<option value="Quatro">Quatro</option>
</select>
<br><br>
<input type="text" id="mysel">
</form>
</body>
</html>

Stamp out, eliminate and abolish redundancy!
 
Niteowl, thanks much. That'll work nicely and is the approach I'll be taking with this project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top