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 PHP-based contact-form, and it works fine, but I need it to send 3 or 4 different emails. How can I do that?
Here is my code:
<table class="table table-striped row-highlight table-condensed">
<thead>
<tr>
<th class="shortcut-base"><a>Base Key</a></th>
<th class="shortcut-binding"><a>Key Binding</a></th>
<th class="shortcut-cmd-id"><a>Command ID</a></th>
<th class="shortcut-cmd-name"><a>Command Name</a></th>
<th class="shortcut-orig"><a>Origin</a></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>Contact us</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="style.css">
</head>
<body id="thankyou">
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "shovo654#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['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 -->
<div class="tahnkyou_page">
<div class="inner_thank">
<p>Thank you for contacting us. We will be in touch with you very soon.</p>
Home
</div>
</div>
</body>
</html>
<?php
}
?>
<div class="effect_form">
<form name="contactform" method="post" action="send_form_email.php">
<fieldset >
<div class='container fastname'>
<input type="text" name="first_name" maxlength="50" placeholder="Fast Name*">
</div>
<div class='container lastname'>
<input type="text" name="last_name" maxlength="50" placeholder="Last Name*">
</div>
<div class='container email'>
<input type="text" name="email" maxlength="80" placeholder="Email*">
</div>
<div class='container phone'>
<input type="text" name="telephone" maxlength="30" placeholder="Phone">
</div>
<div class='container message'>
<textarea name="comments" maxlength="1000" cols="25" rows="6" placeholder="Message*"></textarea>
</div>
<div class="effect_form_bottom">
<div class="eff_form_left">
<p class="phone">An AGE technician will contact you within 24 hours with a free no obligation quote</p>
<p class="lock">Your privacy is important, we do not provide information to third parties.</p>
</div>
<div class="eff_form_right">
<input type='submit' name='Submit' value='Get Free Quote' />
</div>
</div>
</fieldset>
</form>
</div>
So what needs to be done? I've tried, but I can't make it work.
To send an email to multiple recipients :
// multiple recipients
$email_to = 'aidan#example.com' . ', '; // note the comma
$email_to .= 'wez#example.com';
[...]
mail($email_to, $email_subject, $email_message, $headers);
Source: PHP.net
Related
I have a contact form that is coded in this way:
HTML CODE
<form enctype="text/plain" action="send_form_email.php" method="post" role="form">
<div class="col-md-1 col-sm-1"></div>
<div class="col-md-10 col-sm-10">
<div class="col-md-6 col-sm-6">
<input name="Emri" type="text" class="form-control" id="name" placeholder="Emri">
</div>
<div class="col-md-6 col-sm-6">
<input name="Email" type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="col-md-12 col-sm-12">
<input name="Sbjekti" type="text" class="form-control" id="subject" placeholder="Subjekti">
</div>
<div class="col-md-12 col-sm-12">
<textarea name="Msazhi" rows="5" class="form-control" id="message" placeholder="Mesazhi"></textarea>
</div>
<div class="col-md-6 col-sm-6">
<input name="Dergo" type="submit" class="form-control" id="submit" value="DERGO">
</div>
</div>
<div class="col-md-1 col-sm-1"></div>
</form>
I have connected it with a php code called send_form_email.php
PHP CODE
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xhesjanatopalli#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['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);
?>
<?php
}
?>
I have added my email address, but this is not working, when i click submit, send_form_email.php is being downloaded.
Can anyone please help me?
Thanks a lot
PHP is a server-side scripting language, are you running your code in a server that support PHP?
If not, you can easily setup your PHP enviroment with a software like XAMPP
Good morning,
I have added the URL location at the bottom of the PHP file, but still the page does not revert back to .index.html.
Very frustrating.
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "soultrainradio#outlook.com";
$email_subject = "SoulTrain Radio";
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);
("location:http://www.garyhornephotography.com/str/index.html");
?>
<!-- place your own success html below -->
Thank you for contacting SoulTrain Radio. We will be in touch with you very soon.
<?php
}
die();
?>
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="3; url=http://www.garyhornephotography.com/str/index.html">
</head>
<body>
<p>Submitted successfully</p>
</body>
</html>
Good evening everyone, I have added a contact form to my website and when completed, the email assigned, gets the details, however despite my coding efforts and referring to google and on here, I cannot get the blank page thats confirms thanks to revert back to the .index.html page.
<?php if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to="soultrainradio#outlook.com";
$email_subject="SoulTrain Radio";
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);
?> <!-- place your own success html below --> Thank you for contacting SoulTrain Radio. We will be in touch with you very soon. <?php
}
die();
?> ?><!DOCTYPE html> <html> <head> <meta http-equiv="refresh" content="3; url=http://www.garyhornephotography.com/str/index.html"> </head> <body> <p>Submitted successfully</p> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>Welcome to SoulTrain Radio</title>
</head>
<body>
<form name="htmlform" method="post" action="html_form_send.php">
<table width="500px">
</tr>
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="60" size="50">
</td>
</tr>
<tr>
<td valign="top" ">
<label for="last_name ">Last Name *</label>
</td>
<td valign="top ">
<input type="text " name="last_name " maxlength="60 " size="50 ">
</td>
</tr>
<tr>
<td valign="top ">
<label for="email ">Email Address *</label>
</td>
<td valign="top ">
<input type="text " name="email " maxlength="100 " size="50 ">
</td>
</tr>
<tr>
<td valign="top ">
<label for="telephone ">Telephone Number</label>
</td>
<td valign="top ">
<input type="text " name="telephone " maxlength="60 " size="50 ">
</td>
</tr>
<tr>
<td valign="top ">
<label for="comments ">Comments *</label>
</td>
<td valign="top ">
<textarea name="comments " maxlength="1000 " cols="50 " rows="10 "></textarea>
</td>
</tr>
<tr>
<td colspan="2 " style="text-align:center ">
<input type="submit " value="Submit ">HTML Form
</td>
</tr>
</table>
</form>
</body>
</html>
Append >
header("Location: somedomain.com/index.html");
to the end of your PHP file.
My form doesn't generate an E-Mail it just redirects me to a blank page.
i have my .php form in a folder named php on my server
thank you for your help.
here is my html code
<form id="form" method="post" action="php/send_form_email.php">
<fieldset>
<label><strong>Name:</strong>
<input type="text" value="">
</label>
<label><strong>Email:</strong>
<input type="text" value="">
</label>
<label><strong>Phone:</strong>
<input type="text" value="">
</label>
<label><strong>Message:</strong>
<textarea></textarea>
</label>
<div class="btns">ClearSend</div>
</fieldset>
</form>
here is the php code i'm using:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "###.com";
$email_subject = "havok security contact form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there was an error 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['name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$comments = $_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,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 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 .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".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
}
?>
The input type under email needs to have a name attribute called "email". Your PHP script is looking to see if $_POST["email"] is set, and it is not. So, this should work:
<form id="form" method="post" action="php/send_form_email.php">
<fieldset>
<label><strong>Name:</strong>
<input type="text" name="name" value="">
</label>
<label><strong>Email:</strong>
<input type="text" name="email" value="">
</label>
<label><strong>Phone:</strong>
<input type="text" name="phone" value="">
</label>
<label><strong>Message:</strong>
<textarea name = "message"></textarea>
</label>
<div class="btns">ClearSend</div>
</fieldset>
</form>
I am very new at php but even so, this does not make any sense to me:
I have an HTML page containing the php form as follows:
...
<article id="content"><div class="ic">Here comes the contact form!</div>
<div class="wrapper">
<h2>Contact form</h2>
<form method="post" id="ContactForm" name="myemailform" action="sendemail.php">
<div>
<div class="wrapper">
<span>Nume:</span>
<div class="bg"><input type="text" class="input" name="myname"></div>
</div>
<div class="wrapper">
<span>Adresa:</span>
<div class="bg"><input type="text" class="input" name="myadresa" ></div>
</div>
<div class="wrapper">
<span>Email:</span>
<div class="bg"><input type="text" class="input" name="myemail" ></div>
</div>
<div class="textarea_box">
<span>Mesaj:</span>
<div class="bg"><textarea name="textarea" cols="1" rows="1" name="mymesaj"></textarea></div>
</div>
<a href="#" class="button1" name='submit' value="submit" onclick='javascript: document.myemailform.binset=2; document.myemailform.submit();'>Send message</a>
</div>
</form>
</div>
</article>
And the action sendemail.php is as follows:
<?php
if(isset($_POST['submit'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myreal_email#email.com";
$email_subject = "Message from contact form";
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['myname']) ||
!isset($_POST['myadresa']) ||
!isset($_POST['myemail']) ||
!isset($_POST['mymesaj'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['myname']; // required
$last_name = $_POST['myadresa']; // required
$email_from = $_POST['myemail']; // required
$comments = $_POST['mymesaj']; // 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 .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Adresa: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Mesaj efectiv: ".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
}
?>
Now, the problem is that I receive the email, the subject is ok, the sender email is ok, but the message is empty... no $email_message is received.
Why is this happening?
Any help would be appreciated.
Thank you
the problem is with your
<textarea name="textarea" cols="1" rows="1" name="mymesaj"></textarea>
there are two names with the textarea check it.
<textarea cols="1" rows="1" name="mymesaj"></textarea>
if you are usinh HTML 5 or higher version...use 'required' in the input tags...which will automatically validate the input data is present or not...
like this:
<input type="text" class="input" name="myname" required>
and for email validation give the input type as email...not text
like this:
<input type="email" class="input" name="myemail" required >
Also try ajax,jquery for further form validations..
Hello,
I am wondering HOW I can have any and all errors (when the form is submitted) link to a general error page "Link an error.html". The php form I am using now, links to itself and is just an ugly page with no css formatting. Do I need the echos to link to an "error.html"?
I am working with the following HTML FORM :
<div id="contact">
<form name="contactform" method="post" action="send_form_email.php">
<label for="first_name">First <span class="red">*</span></label>
<input type="text" name="first_name" maxlength="50" size="30">
<label for="last_name">Last <span class="red">*</span></label>
<input type="text" name="last_name" maxlength="50" size="30">
<label for="email">Email <span class="red">*</span></label>
<input type="text" name="email" maxlength="80" size="30">
<label for="telephone">Telephone</label>
<input type="text" name="telephone" maxlength="30" size="30">
<label for="comments">Message <span class="red">*</span></label>
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
<input type="submit" value="Send Message">Email Form
</form>
</div>
This is the PHP File that I am Using :
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "info#glustik.com";
$email_subject = "Project Quote Request";
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);
?>
<!-- MY THANK YOU SUCCESS PAGE GOES HERE -->
<?php
}
?>
It is better to show the form validation errors along with the form. That way the user will get direct feedback.
You can use an array for all error messages.
<?php
$errors = array();
if(!isset($_POST['email'])) {
$errors['email'] = 'Email is required';
}
//similarly for other errors.
Now in the form
<input type="text" name="last_name" maxlength="50" size="30">
<label for="email">Email <span class="red">*</span></label>
<?= if(isset($errors['email'])) echo $errors['email'] ?>