0

Short question how do I parse values from options in a form ?

I have this form group

<div class="form-group form-group-lg form-group-select-plus">
<label>Rooms</label>
<div class="btn-group btn-group-select-num" data-toggle="buttons">
  <label class="btn btn-primary active    ">
    <input type="radio" name="rooms" value="1"/>1</label>
  <label class="btn btn-primary">
    <input type="radio" name="rooms" value="2"/>2</label>
  <label class="btn btn-primary">
    <input type="radio" name="rooms" value="3"/>3</label>
  <label class="btn btn-primary">
    <input type="radio" name="options" />3+</label>
</div>
<select class="form-control hidden">
    <option name="rooms" value="1">1</option>
    <option name="rooms" value="2">2</option>
    <option name="rooms" value="3">3</option>
    <option name="rooms" value="4" selected="selected">4</option>
    <option name="rooms" value="5">5</option>
    <option name="rooms" value="6">6</option>
    <option name="rooms" value="7">7</option>
    <option name="rooms" value="8">8</option>
    <option name="rooms" value="9">9</option>
    <option name="rooms" value="10">10</option>
    <option name="rooms" value="11">11</option>
    <option name="rooms" value="12">12</option>
    <option name="rooms" value="13">13</option>
    <option name="rooms" value="14">14</option>
</select>
</div>

I can submit the values in the input fields without a problem but how do I parse the once in the option fields. Assuming the name I want to parse is "rooms"

I'm using PHP to process the form data.

Troels Johannesen
  • 725
  • 2
  • 7
  • 30

2 Answers2

0

You can access the values of rooms by using $_GET['rooms'] if the form is submitted using get. Else there still is $_POST or if you are unsure, try var_dumping $_REQUEST.

See https://stackoverflow.com/a/1924958/151509 for more on that.

Community
  • 1
  • 1
maryisdead
  • 1,792
  • 2
  • 19
  • 30
0

<select>'s hold the name attribute, not the <option>'s

<select class="form-control hidden" name="rooms">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4" selected="selected">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
</select>

Then your handler will take the value chosen from your form's select.

Assuming a POST method:

$rooms = $_POST['rooms'];

Assuming a GET method:

$rooms = $_GET['rooms'];
  • However, you have radio buttons with the same name attribute. You will need to set each element as a unique name if you intend on using all of those together with the select.
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141