I have created a form that submits successfully to an email address, however, it redirects to another page with the text “Thank you. We will be in touch shortly” or “Please fill in all fields and submit again!” as I have used some simple validation.
I would like the form to submit but stay on the same page with the text “Thank you. We will be in touch shortly” or “Please fill in all fields and submit again!”. Any help would be appreciated.
Here is the code written in a file called contact.php:
[php]<?php
if($_POST){
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$number = $_POST[‘number’];
$message = $_POST[‘message’];
$headers = “From: $email \n”; //Set from email address
$mess = "Hello!
Your contact form has been submitted by:
name: $name
number: $number
message: $message
End of message
";
$to = "[email protected]"; //Set your to email address
//validating the fields if any empty
if($name != “” && $email != “” && $number != “” && $message != “”){
mail($to, $subject, $mess,$headers); //calling php mail function
print “Thank you. We will be in touch shortly”;
}
else
{
echo “Please fill in all fields and submit again!”;
}
}
?>
[/php]Here is the form written in a file called contact.html:
[php]
form name=“registration_form” id=“registration_form” class=“form-horizontal” form action=“contact.php” method=“post”>
Name:
</div><!--/form-group-->
<div class="form-group">
<div class="col-sm-4">
<label for="email">Email:</label><br>
<input type="text" class="form-control input-group-lg reg_name" name="email" id="email">
</div>
<div class="col-sm-4">
<label for="number">number:</label><br>
<input type="number" class="form-control input-group-lg reg_name" name="number" id="number">
</div>
</div><!--/form-group--><p><p>
<div class="col-sm-12">
<div class="form-group">
Message:
[/php]