Redirect page Based on the select box while submit the form using Javascript

Here we will see how to redirect page after submit the form based on select box using simple javascript. This is one of the concept redirect a different website based on the select box (drop down box) in the form, For that we should remove default action from the form using the simple javascript code below,

<script>
document.getElementById('comment-form1').addEventListener('submit',(e)=>{
e.preventDefault();
window.location = (document.getElementById('option').value);
})
</script>


The above code is to remove default form submission and helps to redirect page using the option value, you should give the ulr in the select box option value. This is one of the requirement for me from my company  form redirect the different url based on the select box and also send the form information to admin email  id after submission. 


Code for Redirect page Based on the select box while submit the form 


index.php:

<html>
<head>
<title>Redirect page based on select box using javascript</title>
<style>
input, select
{
width: 280px;
margin: 19px;
height: 45 px;
padding: 10px;
}
</style>
</head>
<body>
<form  id="comment-form1" name="contact-form1"  onsubmit="return mysubmit();">
<input type="text"  name="name" placeholder="Your Name.." required><br>
<input type="text"  name="email" placeholder="Your Email.."required></br>
<input type="text"  name="phone" placeholder="Your 10 digit Phone.."require pattern="[0-9]{10}"><br/>
<select  id="option" class="chosen" >
<option selected disabled>Select Bank</option>
<option value="http://demos.sanwebcorner.com/">Sanwebcorner Demo</option>
<option value="http://www.sanwebcorner.com/" >Sanwebcorner Main Page</option>
<option value="http://www.sanwebcorner.com/2018/12/bounce-effect-on-hover-button-using-css-and-html.html" >Single page</option>
</select><br/>
<input name="submit" type="submit" value="Submit" /><br>
</form>

<script>
document.getElementById('comment-form1').addEventListener('submit',(e)=>{
e.preventDefault();
window.location = (document.getElementById('option').value);
})
</script>

</div>
</div>
</section>
</body>
</html>






Redirect Page based on select box and send form information to Admin Email Id:

You can use the below code if you want to send information to your admin email id and also to redirect the page url based on the dropdown box  after send the information to the admin mail. This is the redirect process based on the simple if condition, this conditions will execute after the email function executed.  This is the concept when you choose the particular net banking it should redirct the netbanking url. I hope this two concept will help you . I did'nt provide download option to this concept. pls use the below code it will works fine for me. I provided full code below.

<?php
if (isset($_POST['submit']))

{
$to = "admin@example.com";
$from = "admin@example.com";
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$bank=$_POST['bank'];
$subject = "New Donation Info";
$message = " \n\n Name: ".$name."<br/> \n\n Email: " .$email."<br/> \n\n Mobile: ".$phone."<br/> Bank Name: ".$bank. "<br/> \n\n" ;

//$message = " Hi this is test" ;
$headers =  'MIME-Version: 1.0' . "\r\n";
$headers .= 'From:Donation Info from Website:>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);
echo '<script>alert("Your Enquiry Received . We will contact you soon...");</script>';
if($bank=="www.icicibank.com/safe-online-banking/safe-online-banking.page?itm=nli_hp_0_btn_ib_loginbtn")
{
echo '<script>location.href = "https://www.icicibank.com/safe-online-banking/safe-online-banking.page?itm=nli_hp_0_btn_ib_loginbtn"</script>';
}

elseif($bank=="netbanking.hdfcbank.com/netbanking/?_ga=2.196148587.673827790.1549438128-1636006557.1549438128")
{
echo '<script>location.href = "https://netbanking.hdfcbank.com/netbanking/?_ga=2.196148587.673827790.1549438128-1636006557.1549438128"</script>';

}


elseif($bank=="netbanking.canarabank.in/entry/ENULogin.jsp")
{
echo '<script>location.href = "https://netbanking.canarabank.in/entry/ENULogin.jsp"</script>';
}


}
?>


<html>
<head>
<title>Redirect page based on select box using javascript</title>
<style>
input, select
{
width: 280px;
margin: 19px;
height: 45 px;
padding: 10px;
}
</style>
</head>
<body>
<form  id="comment-form1" name="contact-form1" method="post"  onsubmit="return mysubmit();">
<input type="text"  name="name" placeholder="Your Name.." required>
<input type="text"  name="email" placeholder="Your Email.."required>
<input type="text"  name="phone" placeholder="Your 10 digit Phone.."require pattern="[0-9]{10}">

<select name="bank"  id="option" class="chosen" style="width:500px;">
<option selected disabled>Select Bank</option>
<option value="netbanking.hdfcbank.com/netbanking/?_ga=2.196148587.673827790.1549438128-1636006557.1549438128">Hdfc Bank</option>
<option value="www.icicibank.com/safe-online-banking/safe-online-banking.page?itm=nli_hp_0_btn_ib_loginbtn" >ICICI Bank</option>
<option value="netbanking.canarabank.in/entry/ENULogin.jsp" >Canara Bank</option>
</select>
<input name="submit" type="submit" value="Submit" /><br>
</form>

</div>
</div>
</section>
</body>
</html>

Post a comment

0 Comments