The hiding and removing process can be executed with PHP programming. In this tutorial, we will create a contact form and hide after form submitted. If you are creating an insert form or contact form and want to hide form on same page after form submitted then you can use simple method of PHP programming. In this tutorial, we will discuss about that PHP method to hide form after submitted. Here , we will use PHP function isset() and a variable . When a user fills the form and clicks on the submit button, the form will be hidden from the page. We will set a variable after button clicked and will use that variable with isset() method to hide or remove contact form or you can use this method for any insert form .
Let’s implement the process
How to hide form after submitted in PHP ?
Create a form.php file. In this form file , we will create a HTML form with PHP scripts .We will create a hide variable to set a variable when the button clicked. The main important part to hiding a form using PHP is if condition and PHP isset() function. We will check when the hide variable set then condition will be false and if block will not be executed.
form.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Contact Form </title>
</head>
<body>
<div class="container">
<?php
if(isset($_POST['submit_form']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];
$hide=2;
//your insert query or mail function
echo '<div class="success">Thank you <strong>'.$name.',</strong> Your message has been sent ... </div> ';
}
?>
<?php if(!isset($hide)) { ?>
<h2>Contact Form ⛳</h2>
<form action="" method="POST">
<label> Name </label>
<input type="text" name="name" class="form-control" required>
<label>Email </label>
<input type="email" name="email" class="form-control" required>
<label> Message </label>
<textarea name="msg" cols="10" rows="5" class="form-control" required></textarea>
<input type="submit" name="submit_form" value="Send" class="btn-primary">
</form>
<?php }?>
</div>
</body>
</html>
In the example above , we have created a contact form using HTML also PHP scripts on the same page . We have set variable when the submit button is pressed and isse() for checking variable is set or not . The form is created inside the if block (block of code) . When the variable is set, the block will not execute. This means that on pressing the button, the variable will be set and the form will be hidden because we have created condition that when hide variable is not set then block is executed and if hide variable is set then block will not execute, it means form is inside block then form will be removed . When the form is hidden a message will be shown. You can set any message (Statement) using PHP echo .
We have used CSS stylesheet to design contact form .
<style type="text/css">
body{
background-color: #f1f1f1;
}
.form-control {
width: 100%;
height: 34px;
padding: 6px 12px;
font-size: 14px;
color: #555;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 4px;
}
.btn-primary {
padding: 6px 12px;
font-size: 14px;
font-weight: 400;
cursor: pointer;
border: 1px solid transparent;
border-radius: 4px;
background-color: #337ab7;
color: #fff;
}
textarea.form-control {
height: auto;
}
.container
{
margin-left: 32%;
width: 400px ;
margin-top: 10%;
}
label {
font-size: 18px;
}
.success
{
margin: 5px auto;
border-radius: 5px;
border: 3px solid #fff;
background: #33CC00;
color: #fff;
padding: 10px;
box-shadow: 10px 5px 5px grey;
}
</style>
Use CSS inside the head tag .
In this way , you can hide or remove form after form submitted and show a message on same page .
This is a method to show thank you message or other insert success message without creating extra page in PHP . You can use this PHP method to show thank you message on the same page without any extra thank you page or success message on the same page without any other page .
Recommended Posts:-