j00m
j00m

Reputation: 501

Select option change URL

I have a select form element with values from 1-7+. If the user selects "7+" I want the URL to go to emailForm.php. How can I get this to work? The other options will be captured when the user click a submit button.

<select id="replyNumber">                           
<option selected="true" disabled="disabled" value="">No of people</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7+" onchange="document.location.href=emailForm.php">7+</option>
</select> 

Upvotes: 0

Views: 3609

Answers (2)

madhu131313
madhu131313

Reputation: 7386

You cannot add onchange to option but need to add it to select tag.

You can add on a function changePage and you can check value and then implement the page change like below

   <script>
      function changePage() {
        var val = document.getElementById("replyNumber").value;
        if (val == "7+") {
          document.location.href='emailForm.php'
        }
      }
    </script>
    <select id="replyNumber" onchange="changePage()">
      <option selected="true" disabled="disabled" value="">No of people</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7+">7+</option>
    </select>

Upvotes: 2

dunnel123
dunnel123

Reputation: 384

You can solve this in JavaScript.

First add an onchange=testFunction(this) attribute to your select tag.

Then create and define a function in your JavaScript like follows:

function testFunction(el) {
    if (el.value === '7+') {
        // change the webpage you're currently on
        document.location.href = 'emailForm.php';

        // change the url your form posts to
        document.form-name.action = 'emailForm.php';
    }
}

Whenever a user changes your select box it'll call this function, you can then choose what values to check for, what to do when certain values are selected without submitting the form itself.

Upvotes: 1

Related Questions