PHP FORM SUBMIT BUT STAY ON SAME PAGE

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]

Ajax. You send the form data to a page, it responds, you display the response. No page reloading, no redirection.

The OP didn’t mention about the page reloading. Ajax is not needed. All the processing code goes at the top of the page, form at the bottom, submit to the same page. Check for POST and do the processing. That is standard form processing.

I was hoping to have the php and html in separate files i.e contact.html and contact.php

Could someone let me know if this is possible.

Yes, you would include the file.

I have done that and the form submits successfully, however, the page redirects to a blank page with thank you message or please fill in all fields.

The file is included in the form action i.e form action contact.php

Feel free to actually learn what “include” means.

http://php.net/manual/en/function.include.php

http://www.w3schools.com/php/php_includes.asp

Got it to work. Thank you.

One last thing. I have four pages on my website:

Index.html
Register.php
contact.php
about.html.

Is this OK having html and php links/pages.

Kind regards
Sam

Is this OK having html and php links/pages.

Yes, just remember, Php code will not work in .html files. (Actually, it can if you modify the server to parse .html but best not to do that.)

Thanks for your help. This forum is great. Much better than stackoverflow

Sponsor our Newsletter | Privacy Policy | Terms of Service