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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help getting items from listbox to POST to another page

Status
Not open for further replies.
May 13, 2005
56
US
Hi there,

I need to find a way to get items that have been moved into a listbox caputred and sent as POSTed items to a php page to be entered into a database.

I will be using a submit button to send other data to this page (IE text fields etc..), but I dont know how to get the listbox data over??

Would I be right in thinking that I would want to use javascript to capture these items at time of submit, places the items in hidden fields, thus passing them to the new form?

If this is the right logic, how would I go about doing this?

Thanks,

Mike
 
You should not need any JS to do this. As long as the select element is inside the form element, the POST should submit all the data when the form is submitted.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
The listbox is within the <form></form> tags..

But the variables are still not passed..

I am checking them with this code.

<?
print '<html><body><pre>';

print_r ($_POST);

print '</pre></body></html>';
?>
 
>Would I be right in thinking that I would want to use javascript to capture these items at time of submit, places the items in hidden fields, thus passing them to the new form?
>If this is the right logic...
I think so, one way or the other you need to enlist all the options in order to let the action php(?) page know.

>...how would I go about doing this?
Just put the options value into a string written to the hidden text box. Specify an appropriate separator (not to collide with option values) and let the server-side code split it up. This is an illustration.
[tt]
<html>
<head>
<script language="javascript">
function sync() {
var s="";
for (var i=0;i<document.frmtest.osel.length;i++) {
s+=document.frmtest.osel.options.value+((i==document.frmtest.osel.length-1)?"":",");
}
document.frmtest.alloptions.value=s;
}
</script>
</head>
<body>
<form name="frmtest" method="post" action="youraction.php" onsubmit="sync()">
<select name="osel" multiple>
<option value="abc">123</option>
<option value="def">456</option>
<option value="ghi">789</option>
<option value="jkl">012</option>
</select>
<input type="hidden" name="alloptions" />
<input type="submit" name="tosubmit" />
</form>
</body>
</html>
[/tt]
 
Apologies - I should have said that selected items in the listbox would be passed. Maybe you could write JS to simply select all elements on form submission?

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top