/    Sign up×
Community /Pin to ProfileBookmark

[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.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@qjensenSep 16.2010 — 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.
Copy linkTweet thisAlerts:
@iamanubcakeauthorSep 16.2010 — 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.
Copy linkTweet thisAlerts:
@qjensenSep 16.2010 — 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;[/CODE]


Then every item in the list will be submitted to the server.
Copy linkTweet thisAlerts:
@iamanubcakeauthorSep 16.2010 — 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;
}
}[/CODE]


Is there anything wrong with that code at all? How do I display it on the next page?
Copy linkTweet thisAlerts:
@qjensenSep 16.2010 — 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>[/CODE]


and the php that I have just prints the array from the form values

[CODE]<?php
print_r($_POST['cars']);
?>[/CODE]


returns

[CODE]Array ( [0] => volvo [1] => saab [2] => mercedes [3] => audi )[/CODE]
Copy linkTweet thisAlerts:
@iamanubcakeauthorSep 16.2010 — Thank you very much. It works perfectly fine.
×

Success!

Help @iamanubcake spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...