0

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?

Axel
  • 3,331
  • 11
  • 35
  • 58
user3340037
  • 731
  • 2
  • 8
  • 23
  • The way you've got is working? What's the problem? – Pointy Sep 02 '14 at 21:28
  • just copy the entire ` – Marc B Sep 02 '14 at 21:30
  • I'm a bit confused about your question. What do you mean notvalid input type? The select field is a valid input type. Please give an example. – Yahya Uddin Sep 02 '14 at 21:43

2 Answers2

1

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:

  1. I added a name value to the select with square brackets to show its an array
  2. I added a method type to the form: method="post"
  3. Submit button

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:

How do you extract POST data in Node.js?

Community
  • 1
  • 1
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
0

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.

rybo111
  • 12,240
  • 4
  • 61
  • 70