In this tutorial I’ll show you how to redirect a url after form submission using php. What can be achieved by doing so..?? As it is one of the basic requirements of website. Through redirection you can actually divert your user after submitting form to land to particular page of your choice in order to perform certain actions. Redirection benefits you in many ways.

Now in PHP, redirection is done by using header() function as it is considered to be the fastest method to redirect traffic from one web page to another. The main advantage of this method is that it can navigate from one location to another without the user having to click on a link or button.


Watch the live demo or download code from the link given below

redirect form to a particular page on submission


PHP File:  redirect_form.php

This file consists of html form with four fields and a submit button.


<!DOCTYPE html>
<html>
<head>
<title>Redirect Form To a Particular Page On Submit - Demo Preview</title>
<meta content="noindex, nofollow" name="robots">
<link href='css/redirect_form.css' rel='stylesheet' type='text/css'> <!--== Include CSS File Here ==-->
</head>
<body>
<div class="main">
<div class="first">
<h2>Redirect Form To a Particular Page On Submit using PHP</h2>
<form action="redirect_form.php" id="#form" method="post" name="#form">
<label>Name :</label>
<input id="name" name="name" placeholder='Your Name' type='text'>
<label>Email :</label>
<input id="email" name="email" placeholder='Valid Email Address' type='text'>
<label>Contact :</label>
<input id="contact" name="contact" placeholder='Contact' type='text'>
<label>Address:</label>
<input id="address" name="address" placeholder='Address' type='text' value="">
<input id='btn' name="submit" type='submit' value='Submit'>
<!---- Including PHP File Here ---->
<?php
include "include/redirect.php";
?>
</form>
</div>
</div>
</body>
</html>

PHP Script: redirect.php

When user fills all fields and clicks on submit button in redirect_form.php, this PHP code will executes and redirects to url mentioned in the code.


<?php
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$address = $_POST['address'];
if($name !=''&& $email !=''&& $contact !=''&& $address !='')
{
//  To redirect form on a particular page
header("Location:https://www.formget.com/app/");
}
else{
?><span><?php echo "Please fill all fields.....!!!!!!!!!!!!";?></span> <?php
}
}
?>

CSS File:  redirect_form.css

For Styling HTML Elements.


@import "http://fonts.googleapis.com/css?family=Droid+Serif";
/* Above line is used for online google font */
h2 {
text-align:center
}
hr {
margin-bottom:-10px
}
span {
color:red;
margin-left:65px
}
div.main {
width:960px;
height:655px;
margin:50px auto;
font-family:'Droid Serif',serif
}
div.first {
width:380px;
height:570px;
float:left;
padding:15px 50px;
background:#f8f8ff;
box-shadow:0 0 10px gray;
margin-top:20px
}
input {
width:100%;
padding:8px;
margin-top:10px;
font-size:16px;
margin-bottom:25px;
box-shadow:0 0 5px;
border:none
}
#btn {
width:100%;
padding:8px;
margin-top:10px;
background-color:#474242;
cursor:pointer;
color:#fff;
border:2px solid #adadad;
font-size:18px;
font-weight:700;
font-family:'Droid Serif',serif;
margin-bottom:15px
}
#btn:hover {
background-color:#adadad;
border:2px solid #474242
}

Conclusion:

This was all about php redirecting using header() function. Hope you liked it. Keep reading our other blog posts. 🙂