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
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I was making a html form and when ever someone submit after filling info in it , it should go to my email "nishancofficial#gmail.com". I uploaded my code into website www.nishan.ga/form.html and i fill information for testing it and when I click Submit it say "Submitted" but I waited for long time but it didn't arrived to my gmail address. Can anyone help me!
This is form.html page
<form name="contactform" method="post" action="send.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="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">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
This is send.php page
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nishancofficial#gmail.com";
$email_subject = "User Email";
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['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
$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 .= "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 us. We will be in touch with you very soon.
<?php
}
?>
What am i missing here.
Your code does work. However, I did notice a small error in your posted code in the form.html. There is an extra " at line 13.
There are other questions on StackOverflow regarding PHP Mail not working you'll need to make sure you have a mail server installed on your server (localhost or other) that allows you to send email with the mail function.
Ok so ive worked on this for an hour or so and I've just gone blind to what the issue is. Can someone point out what I need to change to make the below code setup so that email address entered in the form sends an email to my admin email account on my server, lets call it admin#email.com. Below is the html forma nd the php code.
PHP:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "admin#email.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['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 us. We will be in touch with you very soon.
<?php
}
?>
HTML:
<form name="contactform" method="post" action="send_form_email.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">
<input type="submit" value="Submit"> Email Form
</td>
</tr>
</table>
</form>
You don't need any email provider or third party software ON A WORKING SERVER.
I tested the code and it works without problem on server (but shouldn't work on local machine). Do you work locally or on a server? Here's a note from PHP site:
The Windows implementation of mail() differs in many ways from the
Unix implementation. First, it doesn't use a local binary for
composing messages but only operates on direct sockets which means a
MTA is needed listening on a network socket (which can either on the
localhost or a remote machine). Second, the custom headers like From:,
Cc:, Bcc: and Date: are not interpreted by the MTA in the first place,
but are parsed by PHP. As such, the to parameter should not be an
address in the form of "Something ". The mail
command may not parse this properly while talking with the MTA.
More information: PHP: mail - Manual
OK so after awhile I figured out that i was referencing the wrong email. I was logged into my admin email account on my server and not the user account i created so was not seeing the emails. Stupid enough reason, but working now none the less. Thanks for your help.
Just wondering if any of you genius's can help me out with some issues im having regarding a php contact form.
Basically, I have this contact form that requires the basic: name, email, comment. The problem is : when the 'submit' button is pressed when the fields are blank, the validation kicks in effectively, but at the expense of 'breaking' the page (i.e. the </body> and </html> tags are not able to be closed).
I feel this is down to the die(); in function died($error).....take a look....any help is greatly appreciated!
Thanks
PHP: (send_email.php file)
<?php
if(isset($_POST['email'])) {
// Email addys
$email_to = "ch#yahoo.co.uk";
$email_subject = "message";
function died($error) {
// ERRORS
echo "<p class=\"content-text3\">I am very sorry, but there were error(s) found with the form you submitted.</p> ";
echo "<p class=\"content-text\">These errors are as follows:</p><br /><br />";
echo "<p class=\"content-text\">$error </p> <br /><br />";
echo "<p class=\"content-text\">Please go back and fix these errors.</p><br /><br />";
die();
}
// validation
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.');
die();
}
$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);
?>
<!-- sent success -->
<p class="content-text">Thank you for contacting me. I will be in touch with you very soon.</p>
<?php
}
?>
Contact form:
<div id="contact-form">
<form id="contactform" method="post" action="send_email.php">
<table width="450px" border="0">
<tr>
<td valign="top">
<label for="first_name" style="color:#999;">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" style="color:#999;">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" style="color:#999; font-size:14px">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" style="color:#999; font-size:14px">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" style="color:#999; font-size:14px">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input id="sub" type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
</div><!--end contact form-->
I assume you're saying that the form isn't outputted when there are errors, correct?
This is because die() terminates the processing of the page, including anything outside of the PHP tags. You might try removing the die() statement from your error, and instead putting the died() function in an if/else statement.
Eg:
if (strlen($error_message) > 0) {
died($error_message);
} else {
// send the email
}
Yes, you have identified your own issue. Don't use die() in the middle of your script. Probably people are going to vote to close this, and I'm not going to rewrite simple code for you, but I'll risk giving you an actual push in the right direction and suggest that this is the format of the script you want:
First off alter your submit button so it has a name attribute:
<input id="sub" type="submit" name="SubmitEmailForm" value="Submit"/>
While this isn't as important here, when you have pages with multiple forms on them, it will allow you to determine which form was submitted.
In your validation script, initialize an errors array variable to be an empty array:
function validateEmailForm() {
$errors = array();
// All validation here
// if an error occurs...
$errors[] = 'Error text here.';
return $errors;
}
This way, you catch all of the validation errors at one time, so you can provide the user a full report of the things they need to fix.
Then your basic structure is:
if (isset($_POST['SubmitEmailForm'])) {
$errors = validateEmailForm();
if (count($errors) == 0) {
// Form was fine, go ahead and process it
// header(..location of your post-processing script or output..);
// Make sure you exit to end processing of script
exit;
} else {
//
// Output a box containing the errors from $errors using a foreach
//
}
}
//
// Output your form markup here.
//
This is the basic framework that most self-posting forms utilize, and you can add embellishment to this pattern pretty easily.
This question already has answers here:
Why are my PHP files showing as plain text? [duplicate]
(7 answers)
Closed 10 years ago.
This is driving me crazy because I had this form before and it did work and now I am not able to make it work.
The problem is that when I press the button "Submit" instead of submit the form, the "ReportIssueForm.php" file opens as a txt file in the browser and I see all the code.
Any help would be highly appreciated.
Here is the HTML code to call the PHP file:
<form name="htmlform" method="post" action="ReportIssueForm.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="first_name"><b>First Name *</b></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"><b>Last Name *</b></label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"><b>Email *<font size='2'></b></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments"><b>Comments*</b></label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="4" style="text-align:center">
<br><br>
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
And here is the code from the php file:
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "myemail";
$email_subject = "Reporting and issue";
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['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
$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 .= "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);
?>
<!-- place your own success html below -->
Thank you for Reporting an issue. We will look into it and try to solve it as soon as possible.<br><br>
<?php
}
die();
?>
You need to check configuration, if your web-server is configured properly to route .php files request to PHP engine.
In this scenario it seems web-server might not be configured properly.
If you are using Apache as web-server, Make sure below 2 lines are un-commented in httpd.conf(apache config fle)
LoadModule phpX_module "d:/wamp/bin/php/phpXXX/php5apacheXX.dll"
AddType application/x-httpd-php .php
Where X means PHP version
I found a simple, free email contact form online. It doesn't do exactly what I want and I was hoping I could get some help from the experts. I would like to know a couple of things. First, I would like when the user clicks 'submit' that an email is sent to me with the filled in information. Currently, that doesn't happen; an email is sent to me upon form submission only when my email address is entered into the 'email' field. I would also like to know how to add another variable to catch, say... 'job title'. I don't need to worry about captcha catching spam, and I'm not going to mess with anymore form validation than already exists in the script I found. Any help would be awesome, guys!
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemail#email.com";
$email_subject = "KARRN poster";
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 us. We will be in touch with you very soon.
<?php
}
?>
HTML
<form name="contactform" method="post" action="send_form_email.php">
<table width="450px">
</tr>
<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">
<input type="submit" value="Submit"> Email Form
</td>
</tr>
</table>
</form>
You can add another field simply by adding it into the form (e.g. <input type="text" name="job-title" />).
You can retreive the value entered into that field in the PHP script with $_POST['job-title'].
The script should send an email to whatever email address is assigned to the $email_to variable.
See the line that says mail($email_to, $email_subject, $email_message, $headers);? You can duplicate that and hardcode your email address and that will ensure you always receive an email when someone submits the form. If you want to change the message contained within, simply create a new variable (say $email_message2 and pass that to the new call to mail()).
As for adding the job_title field, you just need to do something like this: $jobTitle = $_POST['job_title']. You can then append that variable to the email message. On the form you would add <input type="text" name="job_title" />. Look for other instances of $_POST['SOMETHING'] and make sure to add appropriate code for this new field (this is for validation).
If you want it to be able to submit without an email address you should stop requiring emails being sent with this line
if(isset($_POST['email']))
Note that you will have to set a blank value to $email_from later on, if $_POST['email
] wasn't set, so that you don't get a Notice from PHP on this line $email_from = $_POST['email']; // required
To add job title, just add an <input> on the form, and access it via post like the other posted variables. You can require it by adding it to the line that validates the other POSTs.
By the way, if your form is public I highly reccomend putting a captcha on it. Spammers aren't your only concern, you can also be email-bombed by a malicious person. This happened to me so I always recommend a captcha.
Just use elformo.com and be done with it!