I have this type of multiple select dropdown menu I want to add to my form
<form>
...
<select id="example2" multiple="multiple">
<div id="otherChoices">
<optgroup label="Academic" class="blah">
<option value="Engineering" class="blah">Engineering</option>
<option value="Humanities">Humanities</option>
<option value="Life_Sciences">Life Sciences</option>
<option value="Social Sciences_Sciences">Social Sciences</option>
</optgroup>
<optgroup label="Clubs">
<option value="Event">Event </option>
<option value="Meeting">Meeting</option>
<option value="Performance">Performance</option>
</optgroup>
<optgroup label="Personal">
<option value="Discussion">Discussion</option>
<option value="Event">Event</option>
<option value="Food">Food</option>
<option value="Hangout">Hangout</option>
<option value="Trip">Trip</option>
</optgroup>
</div>
</select>
</form>
But I'm not sure what type of input form this is (it's a bootstrap created form but not of a valid input type), so I'm not sure exactly how to send the parameters via a post request. Currently, I'm using jquery to grab the selected values and and fill in a hidden form. Is there a better way about this?
When you submit a form, it passes an array to the next page, specified in the form's action
attribute. If one isn't set, it will submit to the current URL.
By default, forms will submit using GET
unless set the method
attribute to post
.
So, change your <form>
tag to the following:
<form method="post" action="test.php">
And give your select
a name
, like name="test_select"
.
And then your test.php
can look like this:
<?php
print_r($_POST);
(no need for a ?>
closing tag)
This will print the $_POST
array and you'll see how multiple selects work.
You made a few mistakes in your HTML: This is the corrected version:
<form action="test2.php" method="post">
<select name="example2[]" id="example2" multiple="multiple" >
<div id="otherChoices" name="hello">
<optgroup label="Academic" class="blah">
<option value="Engineering" class="blah">Engineering</option>
<option value="Humanities">Humanities</option>
<option value="Life_Sciences">Life Sciences</option>
<option value="Social Sciences_Sciences">Social Sciences</option>
</optgroup>
<optgroup label="Clubs">
<option value="Event">Event </option>
<option value="Meeting">Meeting</option>
<option value="Performance">Performance</option>
</optgroup>
<optgroup label="Personal">
<option value="Discussion">Discussion</option>
<option value="Event">Event</option>
<option value="Food">Food</option>
<option value="Hangout">Hangout</option>
<option value="Trip">Trip</option>
</optgroup>
</div>
</select>
<button type="submit">Submit</button>
</form>
EDIT: This is what I corrected:
You can then access the individual items like this:
echo $_POST["example2"][0];
EDIT: Sorry I realised this is Node.js That one was for PHP. See this link for how to get post variable in node js: