0

I have this code where i want to redirect the same page to a new html page but i cant just do it. The code is for form The html page is with me please help

<form class="white-pink" align="center" name="myForms" method="post">
            <h1>Search Form 
                    <span>Please Find the car in the fields.</span>
                </h1>
                <label>
                    <span>Search Car :</span> 
                <select name="cars">
                    <option value="def" disabled="disabled" selected="selected">Please select a name</option>       
                    <option value="honda">Honda</option>
                    <option value="hyundai">Hyundai</option>
                    <option value="volkswagon">VolksWagon</option>
                    <option value="toyota">Toyota</option>
                </select>
            </label>

            <label> 
                    <input type="submit" name="s1" value="Search" class="button" onclick="validateForm();"/>
            </label>
        </form>

and for the javascript:

<script>
        function validateForm()
        {       var x=document.forms["myForms"]["cars"].value;
                switch (x)
                {
                    case "def":
                        alert("Please choose a Car Compony");
                            break;
                    case "honda":
                            window.location = "honda.html"; 
                        return false;
                            break;
                    case 2:
                            x="Today is Tuesday";
                            break;
                    case 3:
                            x="Today is Wednesday";
                            break;
                    case 4:
                            x="Today is Thursday";
                            break;
                }


        }
    </script>

there is a drop down list for which after selecting the value i redirect to the required html page

Selvaraj M A
  • 3,096
  • 3
  • 30
  • 46
user2500367
  • 19
  • 1
  • 4

3 Answers3

0

I think you want the form not to be submitted. You can achieve it by using type="button" for the button instead of type="submit"

Or you can do it using JS

<script>
        function validateForm(e)
        {       
                e.preventDefault();
                var x=document.forms["myForms"]["cars"].value;
                switch (x)
                {
                    case "def":
                        alert("Please choose a Car Compony");
                            break;
                    case "honda":
                            window.location = "honda.html"; 
                        return false;
                            break;
                    case 2:
                            x="Today is Tuesday";
                            break;
                    case 3:
                            x="Today is Wednesday";
                            break;
                    case 4:
                            x="Today is Thursday";
                            break;
                }


        }
    </script>
Selvaraj M A
  • 3,096
  • 3
  • 30
  • 46
0

after selecting the value i redirect to the required html page - Try this, Added onchange="window.location.href=this.value;"

            <select name="cars" onchange="window.location.href=this.value;">
                <option value="def" disabled="disabled" selected="selected">Please select a name</option>       
                <option value="yourpath/honda.html">Honda</option>
                <option value="hyundai.html">Hyundai</option>
                <option value="volkswagon.html">VolksWagon</option>
                <option value="toyota.html">Toyota</option>
            </select>
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

Check it:

<html>
<head> Sample page </head>
<body>
<form class="white-pink" align="center" name="myForms" method="post">
            <h1>Search Form 
                    <span>Please Find the car in the fields.</span>
                </h1>
                <label>
                    <span>Search Car :</span> 
                <select name="cars">
                    <option value="def" disabled="disabled" selected="selected">Please select a name</option>       
                    <option value="honda">Honda</option>
                    <option value="hyundai">Hyundai</option>
                    <option value="volkswagon">VolksWagon</option>
                    <option value="toyota">Toyota</option>
                </select>
            </label>

            <label> 
                    <input type="button" name="s1" value="Search" class="button" onclick="validateForm();"/>
            </label>
        </form>
<script>
        function validateForm()
        {       var x=document.forms["myForms"]["cars"].value;
                switch (x)
                {
                    case "def":
                        alert("Please choose a Car Compony");
                            break;
                    case "honda":
                            window.location.href = "http://www.honda.com"; 
                            break;
                    case "hyundai":
                            window.location.href = "http://www.hyundai.com"; 
                            break;
                    case "volkswagon":
                            window.location.href = "http://www.volkswagon.com"; 
                            break;
                    case "toyota":
                            window.location.href = "http://www.toyota.com"; 
                            break;
                }
        }
    </script>
</body>
</html>
anik4e
  • 493
  • 8
  • 16