Setup
I have a simple HTML form that points to a PHP script that will send the email.
Problem
When I click submit the email is sent, but I am taken to the PHP url.
I tried to all TARGET="_self" in the and declarations but i was still taken o a new page.
Code
HTML CODE
<form name="contactform" method="post" action="send_form_email1.php">
<table width="450px">
<tr>
<td>
<input type="text" placeholder="Full Name" name="full_name"/>
</td>
</tr>
<tr>
<td>
<input type="text" placeholder="Email Address" name="email"/>
</td>
</tr>
<tr>
<td>
<input type="text" placeholder="Phone Number" name="telephone"/>
</td>
</tr>
<tr>
<td>
<input type="text" placeholder="Question" name="comments"/>
</td>
</tr>
tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit" >
</td>
</tr>
</table>
</form>
PHP SCRIPT
// EDIT THE 2 LINES BELOW AS REQUIRED
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "zachmurray13#gmail.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['full_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$full_name = $_POST['full_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$full_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<?php
}
?>
Any help is greatly appreciated.
Post works by sending the data to the page specified in to form's action. you need to redirect the browser back to the page you want. you can do this by a 302 redirect.
header('Location: http://www.yoursite.com/home-page.html');
exit;
If you don't want to go to the new URL then you can make AJAX request to the php script instead of form submit.
Form element works as sending data to different page. as you have specified in action.
if you want to come back to actual one
you have to redirect to it.
you can also include your send_form_email1.php file to actual one.
for instance:
header('Location: page_url');
or
include('yourfile');
if you are including file to actual file then you have to set action="" blank it automatically send it to same page and you have to check as post request. Then other code will remain same as.
Your browser will load the page as the form is posted to it. This is expected behaviour.
You can workaround this with some options:
The post is done to the same page, the PHP code handling the form is part of it. Check if ($_POST) to know the user is submitting something, but the page will be reloaded anyway;
The page your form is calling will redirect the user to some other page;
Post it using AJAX. This way you don't even lose the fields content, the page isn't refreshed at all.
Option 1 is not preferred because if the user tries to reload the page after submitting the form, the browser will submit it again. The best practice to avoid this is to redirect the user, unless you are posting with AJAX, in this case you have full control of whatever is posted, it doesn't even populate the browser history with them.
I think using header will be the great option. .
header('Location of your html page'); in your php page to handle forms..
There is another option that is not used widely..
You could include the code in php file to your first page and save it as a php code , then in the action attribute of form tag , Please enter no values like
<form name="contactform" method="post" action="">
It is not a recommended one, But for your problem, I strongly recommend this as the most simple solution
You can try a few options
You can try ajax - being on the same page and use another php file to send your email
This would require you to change the extension from .html to .php. You can post the form to the existing page it self. There are some security look holes though.
You can keep your existing code, but redirect back to the initial page once the email has been sent through javascript or php headers.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So, two problems:
1.) My email form is not working, on submit, it advises "web page can't be displayed".
2.) The email form, when properly coded, did not send an email.
I'm trying to get my email form to properly work. It was working, (however was not sending email) then I rearranged some things (added subject line in the form). Now, I can't seem to figure out what went wonky once I added a subject line. Any suggestions?
https://jsfiddle.net/ebxam743/1/
My PHP form is in the CSS section of JSFIDDLE.
Contact Form HTML
<div class="col-md-8">
<form name="contactform" method="post" action="send_form_email.php">
<div class="row contact-row">
<div class="col-md-6 contact-name">
<input type="text" name="first_name" placeholder="Name">
</div>
<div class="col-md-6 contact-email">
<input type="text" name="email" placeholder="E-mail*">
</div>
</div>
<input type="text" name="subject" placeholder="Subject*">
<textarea name="comments" placeholder="Message"></textarea>
<input type="submit" class="btn btn-lg btn-color btn-submit" value="Send Message">
</div>
</div>
</form>
<!-- end col -->
PHP (can't get it to format properly)...
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you#yourdomain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['subject']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<center><img src="img/rebelliouslogob.png"
Thank you for contacting us. We will be in touch with you very soon.</center>
<h1>Go Back</h1>
<?php
}
?>
Okay, so I found 2 errors.
First error
This line:
echo "Please go back and fix these errors.<br /><br />";
Should be changed to:
echo "Please go back and fix these errors.<br /><br />";
Because the quote symbol is breaking the string, so if you need to use the quote symbol inside a string you need to escape it by adding a backslash or create strings using single quotes.
Second error
You are referring to a variable called $subject that doesn't exist.
So instead of using a variable called $telephone which contains the subject data, change the name of it to $subject (you never use the variable $telephone, so it shouldn't affect you).
This line:
$telephone = $_POST['subject']; // not required
Should be changed to:
$subject = $_POST['subject']; // not required
EDIT: Tested the code on my server after the two changes I mentioned above, and I received an email as expected.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a simple form:
<form action="send_form_email.php" method="post" enctype="text/plain">
<input type="text" name="first_name" placeholder="NAME:" size="100" class="grey-bg"/>
<input type="text" name="last_name" placeholder="E-MAIL:" size="100" class="grey-bg"/>
<br />
<br />
<textarea type="text" name="message" rows="20" cols="201" placeholder="MESSAGE:" size="162"></textarea>
<br />
<div>
<input id="contact-send-btn" type="submit" value="Send">
</div>
</form>
And this is the php file:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xxxxxxxx";
$email_subject = "Hello";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['message']) ||
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Message: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
However when I click the button it just opens the php file in the browser and nothing is received in my inbox? All I want is to receive the email with the details in the form?
You have several problems.
Encoding Type
enctype="text/plain"
PHP does not support plain text encoded form data. Remove that attribute (and use the default encoding type).
See also the spec:
Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at the end of the value).
Test for submission
You said if(isset($_POST['email'])) {
… but you don't have any field with name="email". Test to see if a field you actually have exists*
Opening PHP in the browser
This statement is a little ambiguous:
it just opens the php file in the browser
If you mean that your browser displays the PHP source code, then see this question: PHP code is not being executed (I can see it on source code of page)
Asides
placeholder="NAME:"
The HTML5 placeholder attribute is not a substitute for the label element
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
This fails to match many perfectly valid email addresses (such as those with a + in the section before the # or those from the .museum top level domain).
You did not add name attribute to submit button.
Change
<input id="contact-send-btn" type="submit" value="Send">
To:
<input id="contact-send-btn" type="submit" value="Send" name="email"/>
While posting forms, only elements with name get posted.
In your case, submit button has no name attribute, so, it was not getting posted.
Hence, it is not coming into the if.
Looks like you don't have apache , php installed thats why you are seeing the content of the .php file . If you are on Windows Install XAMPP if you are on Linux Install LAMP Stack . Hope this helps
I have researched this question, but I don't think I will find an answer, because I'm guessing it is something wrong with my code personally. I have tried every combination of trying to get my PHP contact form to Submit & send me an e-mail but it does nothing, even when it has been uploaded to the server. It simply displays a blank page once I click submit, and I get no e-mail at all to the e-mail address I entered into the code. I don't know what I'm missing as I can't find an answer to my problem anywhere.
One thing I did notice was that I have some different fields in my HTML than specified in the PHP to be required fields. However, I don't know that this makes a difference as I'm not getting any error messages or anything else for these fields. Please let me know if you need any additional details. I appreciate your help in advance.
HTML
<form id="form_884913" method="post" action="send_form_email.php" class="appnitro">
This is where ALL of my fields are, so I omitted it since quite a large script. Please let me know if you need any of this to help me
Rest of HTML
<input type="hidden" name="form_id" value="884913" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
<input type="reset" value="Reset">
</form>
PHP
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemailaddresswastyped#blank.com";
$email_subject = "Contact";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting me! I will be in touch with you very soon.
<?php
}
?>
As mentioned by GaijinJim, if it isn't already a field you've posted, since you omitted some of the fields, make sure to add a field in HTML:
<input type="hidden" name="email" value="" />
Might also add for testing, but not for production, to the last line in PHP:
<?php
} else {
echo "$_POST['email'] is not set.";
}
?>
If you want to use $_POST (Same with $_GET method) with a form in HTML&PHP you must name your field like so:
<input type="text" name="MyfieldName" value="1">
Only then you can call your $_POST['MyfieldName']
Now if you would echo $_POST['MyfieldName']; it would return 1
In your PHP-file you check whether all of the following input fields exist in your form,
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
Thus in order for your form to validate all of those fields must exist, you must have,
<input type="text" name="first_name" />
<input type="text" name="last_name" />
<input type="text" name="email" />
<input type="text" name="telephone" />
<input type="text" name="comments" />
If you do not want some of those fields, for example email, simply remove `!isset($_POST['email']) from your PHP's if-statement.
As per your comment I get the impression that the input field named email does not exists, which is the reason why you get a blank page. If you were to add that input field on your HTML-page you would see some sort of error message instead of a blank page :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So far I have the basic text input.
<form name="contact" method="post" action="contact.php">
<div id="subscribe">
<input type="text" placeholder="Enter your email address...">
<input type="submit" value="Submit">
<div class="clear"></div>
</div>
This is my code for my "Coming Soon" landing page. What I wan't to figure out is how to have the emails entered send somewhere for me to access. I've tried adding PHP but I fail (I'm net to web development) .
This is the PHP code I'm trying to use
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you#yourdomain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if !isset($_POST['email']) ||
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$email_from = $_POST['email']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<?php
}
?>
Anytime I try nothing happens. If you know a shorter way of doing this please let me know or another way of doing it. Thanks!
What people are saying is:
//<form method="post">
// Why do you have two form starts? Get rid.
<form name="contact" method="post" action="contact.php">
<div id="subscribe">
// you have to give the INPUT a name attribute.
// If you want to access the data using $_POST['email'],
// then you have to name it 'email'
<input name="email" type="text" placeholder="Enter your email address...">
<input type="submit" value="Submit">
<div class="clear"></div>
</div>
</form>
Well, the most obvious problem is you have your entire script testing for a POST vairable that isn't in your form. Make this change to your HTMl and I bet it works.
<input type="text" name="email" placeholder="Enter your email address...">
EDIT
To clarify this some, $_POST is an array filled with the form fields you submits. So $_POST['email'] requires you to submit a form with a field named email. No field name, no easy way to reference it
So I Made a webpage that has a form, and found some relatively straightforward code to take the contents of a form, check it for errors or incomplete sections, and either return errors or send it. Unfortunately, it returns the errors in a new page rather than within the form page itself, which is what I want it to do.
The HTML form:
<form name="contactform" method="post" action="send_form.php">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<h6>An astrisk (*) denotes a required field.</h6>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
The PHP code:
if(isset($_POST['email'])) {
$email_to = "my#email.com";
$email_subject = "Comments";
function died($error) {
//This is where I think the code that prints to the same webpage should be rather than putting on a new page, which is what I does now
echo "There were error(s) found with the form you submitted. Please review these errors and resubmit.";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
<!-- email success html -->
<h4>Thank you for contacting us. We will be in touch with you very soon.</h4>
<h4><a class="submitSuccess" href="index.php">Return to Index</a></h4>
}
I basically want it to print a line of text above the textbox saying that the form wasn't filled correctly. Right now it creates a new webpage with the errors.
side-note:
There should be a few php brackets, but the code wasn't displaying properly with them in, so I took them out. You get the idea of the code though.
Looking at your code, the basic behavior of the PHP page is to check if there are any validation errors. If yes, show an error message, otherwise send an email.
What you want to do is to show the original page with the validation errors.
Usually, at least in almost every framework I know, this is done by using a redirection.
Basically, if there are no errors, we redirect the user to a success page.
If there are errors, we put the error information + the user data in a session variable and redirect to the original form.
The basic code structure would look like this:
the Validation Script:
if ( validate_form($_POST))
{
/*
Form validation succeeded,
Do whatever processing you want (Like sending an email)
*/
header('location: success.php'); // redirect to the success page
}
else
{
/*
Form validation failed
*/
session_start();
$error = ...;
$form_data = array('error' => $error, 'username' => $_POST['username'], ...);
$SESSION['form_data'] = $form_data;
header('location: form.php');
}
the form Script:
<?php
session_start();
if( isset($SESSION['form_data']))
{
$username = $SESSION['form_data']['username'];
$errors = $SESSION['form_data']['error'];
}
else
{
$username = '';
$errors = '';
}
?>
<form name="contactform" method="post" action="send_form.php">
<input type="text" name="username" value=<?php $username ?> >
<label for="errors"><?php $errors ?></label>
</form>
PS: This code is a simple demonstration and is obviously not 100% correct. You have to add more exception handling, more security checks... But you get the idea.
I would recommend that you try to use an lightweight MVC framework to understand how this can be done in a correct way.
You can even look at the source code of these frameworks.
When your forms data don't pass your verification server-side you call the function died() with a die() at the end. Please check the PHP help to be sure that die() does what you need. Cause die tell the PHP to stop parsing PHP and send the page AS-IS to the client. So your form will never appear.
Personnaly: I use die function for debugging purpose ONLY
By saying:
<form name="contactform" method="post" action="send_form.php">
You are telling the HTML form to go to send_form.php in the web browser.
Then, in send_form.php, the logic is done to see if the form's contents are correct, so it displays the echo on that new page.
You're going to have to use some script (javascript/php) in the original HTML page to check if the form is filled out correctly.
See something like this: http://www.thesitewizard.com/archive/validation.shtml