drich
drich

Reputation: 69

How select POST values from different select options?

For example, I have 3 different select options:

<select name="FirstOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

<select name="SecondOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

<select name="ThirdOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

So, when I click a button, I want to get all the values of all 3 select options and insert the values into the database.

Right now, I'm using this button with a href tag but looks like it's very wrong:

 <a href="../reservation-confirmation?tour_id='.$tour->ID.'" class="reserveButton" name="reserveButton" click="return validateRequirement();">Reserve</a>

What's the best way to do this?

Upvotes: 0

Views: 59

Answers (1)

Olufemi
Olufemi

Reputation: 346

  1. Wrap the selects inside a form and put a submit button.
  2. Add your ID as hidden fields.

Your HTML code should now be something like this :

<form method="post" action="../reservation-confirmation">
<select name="FirstOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

<select name="SecondOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

<select name="ThirdOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

<input type="hidden" value="" name="tour_id" >
<button type="submit" name="submit'> Submit </button>
</form>
  1. On the server-side, process the responses by targeting

    if(isset($_POST['submit']))
        $FirstOption$=$_POST['FirstOption']
        $SecondOption$=$_POST['SecondOption']
        $ThirdOption$=$_POST['ThirdOption']
        ...   //Add them into your database. 
    
  2. Remember to use prepared statements.

Upvotes: 1

Related Questions