user6276867 user6276867 - 6 months ago 31
PHP Question

How to redirect form action based on selected option value?

I have a Search Form that will, based on selected option value, redirect the user after clicking on submit button.
Here is my Form code:



<form method="get" action="http://www.yourdomain.com/hotels/search" role="search">
<select>
<option value="">Select Location</option>
<option value="10">USA</option>
<option value="3">Canada</option>
<option value="4">Singapore</option>
<option value="5">India</option>
<option value="6">China</option>
</select>
<button type="submit">Submit</button>
</form>





For example: If we select USA, cause Value is 10, User web page will be redirected to the following URL:


http://www.yourdomain.com/hotels/search/USA/USA/10?


I want to add this specific line after question mark:

checkin=%F%F&checkout=%F%F&adults=0&child=0

Something like this:


http://www.yourdomain.com/hotels/search/USA/USA/10?checkin=%F%F&checkout=%F%F&adults=0&child=0


How can I add it with JavaScript?

I'm looking for JavaScript to make it.

Thanks in advance

Answer

i changed your code and made new jquery function which may help you! please go through it!

      <form method="get" action="" role="search">
      <select id="country">
    <option value="">Select Location</option>
    <option value="10">USA</option>
    <option value="3">Canada</option>
    <option value="4">Singapore</option>
    <option value="5">India</option>
    <option value="6">China</option>
    </select>
    <button type="button" id="buttonId">Submit</button>
    </form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<script type = "text/javascript" language = "javascript">
 jQuery(document).ready(function() {
    jQuery("#buttonId").click(function() {
        var country_value = $('#country').val();
       var country_name = $('#country :selected').text(); 
            window.location = "http://www.yourdomain.com/hotels/search/"+country_name + "/" +country_name +"/" + country_value +"?checkin=%F%F&checkout=%F%F&adults=0&child=0";

    });
 });
</script>