I have two issues with a contact form I have created. I was previously hit hard by spam.
I am requiring that all fields be filled out before the form is processed, but what I have written isn't working: info goes into the database whether a person fills out all fields or not. ***fixed by using:
function validateForm()
{
var x=document.forms["validation"]["firstname"].value;
if (x==null || x=="")
{
alert("Please enter your first name");
return false;
}
for all fields and this one for email:
var x=document.forms["validation"]["email"].value;
var atpos=x.indexOf("#");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Please enter a valid email address");
return false;
}
Now, I need to get the captcha working or how to add to check if captcha is correct in same javascript? I think the error lies in this somehow?:
session_start();
if($_POST['submitted'] == "contactus")
if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
header("Location:http://www.berrieswebdesign.com/thankyou.php?message=thanks");
unset($_SESSION['security_code']);
} else {
// Insert your code for showing an error message here
echo "<div id='thankyoubox'>'Security breach! Security Breach! Ehem...Your security code was incorrect.'</div>";
}
ob_flush();
?>
And lastly, here is contactfunctions.php
<?php ob_start();//Required for the redirect to work?>
<?php
include_once("databasefunctions.php");
$contactsdbtable = "contacts";
function GetHeaders()
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= "To: {$firstname} <{$email}>" . "\r\n";
$headers .= 'From: My Website <info#mywebsite.com>' . "\r\n";
return $headers;
}
function ContactMessage($firstname, $lastname, $email, $message, $location)
{
global $contactsdbtable;
openDatabase();
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$email = mysql_real_escape_string($email);
$message = mysql_real_escape_string($message);
$location = mysql_real_escape_string($location);
$result = QuickQuery("INSERT INTO {$contactsdbtable}(firstname, lastname, email, message, location)
VALUES('{$firstname}', '{$lastname}', '{$email}', '{$message}', '{$location}')");
if($result)
{
$headers = GetHeaders();
$message = "\"Thank you for contacting us at My Website. We will be answering your website inquiry post haste.\"<br />
<br />
<br />
Best Regards,<br />
<br />
Me
";
mail($email, "RE: Design Inquiry", $message, $headers);
mail("myemail#blahblah.com", "Website Inquiry", "{$firstname}, {$email}, has sent a web design inquiry", $headers);
}
}
?>
I appreciate any help I receive on this in advance. Also, since this is a lengthy post, would you guys mind listing which issue you are addressing, 1 or 2?
Thanks!
Ok try this:
<?php
$is_error = false;
if($_POST['submitted'] == "contactus")
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$location = $_POST['location'];
if(!$firstname || $firstname = ''){
$error = "Please enter your first name.";
$is_error = true;
} else if(!$lastname || $lastname= ''){
$error = "Please enter your last name.";
$is_error = true;
} else if(!$email || $email= ''){
$error = "Please enter a valid email.";
$is_error = true;
}else if(!$message || $message= ''){
$error = "Please enter your message.";
$is_error = true;
}else if(!$location || $location= ''){
$error = "Please tell us where you're from.";
$is_error = true;
}
if(($is_error === false) && ($_SESSION['security_code'] == $_POST['security_code']))
{
ContactMessage($firstname, $lastname, $email, $message, $location);
} else {
Error($error);
}
}
?>
Related
All is working fine, except redirect to the thank you page. I can send mail both sides, but after contact form is submitted I can't redirect the page to thankyou.html Am I doing something wrong in the code?
session_start();
$serverMail = 'example#gmail.com';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['mysite'];
$headers = "From: $serverMail\r\n";
$toAdmin = "some example";
$toGuest = "some example";
if(empty($email) or empty($name) or empty($email) or empty($message)){
$output = "All fields are required!";
}
else{
//to server side
if(md5($captcha) == $img_session){
$mailMessage = "
$name
some message";
if (mail($serverMail, $toAdmin, $mailMessage, $headers)) {
}
}
// to guests side
if(md5($captcha) == $img_session){
$mailMessage = "
$name
some message";
if (mail($email, $toGuest, $mailMessage, $headers)) {
header('Location:https://stackoverflow.com/');
exit();
}
}
else{
$output = "Wrong Captcha Code!";
}
}
echo $output;
I have to if(md5($captcha) == $img_session) because $mailMessage are different.
There is no need to test the captcha twice.
Once the captcha is checked, build and send both emails, keeping the status in a temp variable. Once the 2 emails are sent check the status of both and code accordingly for picking up the error whereever it is found.
session_start();
$serverMail = 'example#gmail.com';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['mysite'];
$headers = "From: $serverMail\r\n";
$toAdmin = "some example";
$toGuest = "some example";
if(empty($email) or empty($name) or empty($email) or empty($message)){
$output = "All fields are required!";
} else{
if(md5($captcha) == $img_session){
//to server side
$mailMessage = "\n$name\n\nsome message";
$m1 = mail($serverMail, $toAdmin, $mailMessage, $headers)) {
// to guests side
$mailMessage = "\n$name\n\nsome other message";
$m2 = mail($email, $toGuest, $mailMessage, $headers)) {
if ( $m1 && $m2 ) {
header('Location:https://stackoverflow.com/');
exit;
} else {
// one or both emails failed to send
if( ! $m1 ) {
// error message for server mail failure
}
if ( ! $m2 ) {
// error message for guest mail failure
}
} else {
$output = "Wrong Captcha Code!";
}
}
echo $output;
Try using this code, if it doesnt give try using a different browsers. it may be from the
echo '<META HTTP-EQUIV="refresh" CONTENT="0;url=http://helloworld/contact/thankyou.html">';
Because you use "exit" after the first mail!
I'm currently building a very small 'contact' form for use on a personal site.
The form works, and each validation 'if' statement works individually, however, if for example I input a valid email address and phone number but leave the message blank, the email still sends and I get the success message.
My guess would be to include the small 'if' statements into the one checking whether my required fields are not empty, though i'm not sure how to do this correctly as it is nesting multiple 'if's into one.
Cheers
<?php
// Validation goes here
$errors = '';
$success = 'Success! Your message has been sent. You should receive a reply within 48 hours.';
$email = $_POST['email'];
$name = $_POST['thename'];
$comments = $_POST['comments'];
$number = $_POST['number'];
if(empty($name) || empty($email) || empty($comments)) {
$errors .= "Error: please input a name, email address and your message.";
} else {
$errors = '';
}
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
$errors .= "Error: Invalid email address";
} else {
$errors = '';
}
if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
$errors .= "Error: Invalid phone number";
} else {
$errors = '';
}
?>
<!-- Display red error box or green success box depending on which is true -->
<?php if(!empty($errors)): ?>
<div class="validationbox | errorsbox">
<?php echo $errors; ?>
</div>
<?php elseif(empty($errors)): ?>
<div class="validationbox | successbox">
<?php echo $success; ?>
</div>
<?php
$message = ''; // Blank message to start with so we can append to it.
// Construct the message
$message .= "
Name: {$_POST['thename']};
Email: {$_POST['email']};
Number: {$_POST['number']};
Enquiry-type: {$_POST['enquiry-options']};
Message: {$_POST['comments']};
";
// test#testdomain.com
$to = 'test-email-deleted-for-stackoverflow';
$subject = 'Message from Portfolio';
$from = $_POST['thename'];
// YourSite#domain.com
$fromEmail = 'test-email-deleted-for-stackoverflow';
$header = 'From: ' . $from . '<' . $fromEmail . '>';
mail($to,$subject,$message,$header);
?>
<?php endif; ?>
<?php endif; ?>
Your problem is that you are resetting $errors back to '' each time one of your validation conditions passes:
if(empty($name) || empty($email) || empty($comments)) {
$errors .= "Error: please input a name, email address and your message.";
} else {
$errors = '';
}
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
$errors .= "Error: Invalid email address";
} else {
$errors = '';
}
if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
$errors .= "Error: Invalid phone number";
} else {
$errors = '';
}
You shouldn't do that, just leave error messages to whatever it previously was. This way, when you get to the end, $errors will contain a string of all the error messages combined. Since there could be multiple messages, you may want to put a break a the end of each one:
if(empty($name) || empty($email) || empty($comments)) {
$errors .= "Error: please input a name, email address and your message.<br>";
}
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
$errors .= "Error: Invalid email address<br>";
}
if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
$errors .= "Error: Invalid phone number<br>";
}
In the case of email, you may want to only display the 'invalid email address' only when something was actually filled in, so you could also check to ensure there is something in there, before you determine if the format is valid or not:
if (!empty($email) && !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
Based on the information Supplied, i think you should use a complex if-elseif-else statement like so:
`if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
} `
in your particular case:
// Validation goes here
$errors = '';
$success = 'Success! Your message has been sent. You should receive a reply within 48 hours.';
$email = $_POST['email'];
$name = $_POST['thename'];
$comments = $_POST['comments'];
$number = $_POST['number'];
if(empty($name) || empty($email) || empty($comments)) {
$errors .= "Error: please input a name, email address and your message.";
} elseif(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
$errors = 'Error:invalid email';
}elseif(!preg_match("/^\(?0( *\d\)?){9,10}$/", $number){
$errors .= "Error: Invalid phone number";
} else {
//Do this on successful validation comes here
}
try below code it helps you.
<?php
// Validation goes here
$errors = '';
$success = 'Success! Your message has been sent. You should receive a reply within 48 hours.';
$email = $_POST['email'];
$name = $_POST['thename'];
$comments = $_POST['comments'];
$number = $_POST['number'];
if(empty($name) || empty($email) || empty($comments)) {
$errors .= "Error: please input a name, email address and your message.";
} else {
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
$errors .= "Error: Invalid email address";
} else {
$errors = '';
}
if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
$errors .= "Error: Invalid phone number";
} else {
$errors = '';
}
}
?>
I have a reset password link that seemingly won't process $_GET('variable'). The forgot password form:
<?php
$error = $email = "";
if (isset($_POST['email']))
{
$email = sanitizeString($_POST['email']);
$com_code = md5(uniqid(rand()));
if ($email == "")
$error = "Not all fields were entered<br>";
else if (!filter_var($email, FILTER_VALIDATE_EMAIL))
$error='Email is invalid';
else
{
$resultE = queryMySQL("SELECT email FROM users WHERE email='$email'");
if ($resultE->num_rows == 0)
{
$error = "<span class='error'>Email
error</span><br><br>";
}else
{
queryMysql("UPDATE users SET com_code='$com_code' WHERE email='$email'");
$mail_to = $email;
$subject = 'Reset your password ';
$body_message = 'Please click on this link to reset password ';
$body_message .= 'Activate';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if(isset($_SESSION['url']))
$url = $_SESSION['url'];
else
$url = "../../index.php";
header("Location:$url");
}
}
}
?>
The reset password form:
<?php
$error = $pass ="";
if (isset($_POST['pass']))
{
$pass = sanitizeString($_POST['pass']);
$salt1 = "qm&h*";
$salt2 = "pg!#";
$token = hash('ripemd128', "$salt1$pass$salt2");
$passkey = $_GET['passkey'];
if ($pass == "")
$error = "Enter all fields";
//put if else statements here
else if (preg_match("/[^a-zA-Z0-9_-]/", $pass)){
$error='Remove spaces,numbers,special characters';
}
else
{
$resultpassw = queryMysql("SELECT * FROM users WHERE com_code='$passkey' ");
if ($resultpassw->num_rows == 0)
$error = " ✘ Confirmation not sent";
else
{
queryMysql("UPDATE users SET pass='$token', updated=CURRENT_TIMESTAMP WHERE com_code='$passkey'");
header("Location:../../profile.php");
}
}
}
?>
The error that keeps occurring is the 'confirmation not sent' implying that the table 'users' has no com_code inserted previously, but when I look at the table via phpmyadmin the com_code is there. Where I'm I going wrong
In forgot passaword form try below.
<?php
$error = $email = "";
if (isset($_POST['email']))
{
$email = sanitizeString($_POST['email']);
$com_code = md5(uniqid(rand()));
if ($email == "")
$error = "Not all fields were entered<br>";
else if (!filter_var($email, FILTER_VALIDATE_EMAIL))
$error='Email is invalid';
else
{
$resultE = queryMySQL("SELECT email FROM users WHERE email='$email'");
if ($resultE->num_rows == 0)
{
$error = "<span class='error'>Email
error</span><br><br>";
}else
{
queryMysql("UPDATE users SET com_code='$com_code' WHERE email='$email'");
$mail_to = $email;
$subject = 'Reset your password ';
$body_message = 'Please click on this link to reset password ';
$body_message .= 'Activate';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if(isset($_SESSION['url']))
$url = $_SESSION['url'];
else
$url = "../../index.php";
header("Location:$url");
}
}
}
?>
I changed one line here.
$body_message .= 'Activate';
As $com_code is dynamic value so you need to pass it in way, so php can fetch its value, and not take it as a static value.
Think I'll just use sessions. forgot_pass.php:
$com_code = md5(uniqid(rand()));
$_SESSION["com_code_sesh"] = $com_code;
reset_pass.php:
$passkey = $_SESSION["com_code_sesh"];
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
i dont know why it wont send it just echo's this msg Oops.. There seems to be a technical errorWe are truly sorry, please check again later
i dont know whats the problem maybe i'm overlooking,
can u pls tell me i have i gone wrong.
if you need the html code let me know
suggestions on how to improve this code would be gratefully appreciated :)
PHP CODE:
<?php
function clean($str)
{
$str = mysql_real_escape_string($str);
$str = htmlspecialchars($str);
$str = strip_tags($str);
return($str);
}
if(isset($_POST['name']) &&
isset($_POST['address']) &&
isset($_POST['hp']) &&
isset($_POST['email']) &&
isset($_POST['rm']) &&
isset($_POST['service']) &&
isset($_POST['name-owner']) &&
isset($_POST['name-rep']) &&
isset($_POST['contract-from']) &&
isset($_POST['contract-to']) &&
isset($_POST['agree']) &&
isset($_POST['submit'])){
$name = clean($_POST['name']);
if($name == NULL || $name == "")
{
die ("Please enter your name");
$errormsg = "<button class=\"\" onClick=\"history.go(-1)\"> Retry</button> ";
}
$address = clean($_POST['address']);
if($address == NULL)
{
die ("Please enter your Business address");
$errormsg;
}
$hp = clean($_POST['hp']);
if($hp == NULL)
{
die ("Please enter your Phone No.");
}
$email = clean($_POST['email']);
if($email == NULL)
{
die ("Please enter your Email address");
$errormsg;
}
$url = clean($_POST['url']);
$rm = clean($_POST['rm']);
if($rm == NULL)
{
die ("Please enter your Amount/Percentage");
$errormsg;
}
$service = clean($_POST['service']);
if($service == NULL)
{
die ("Please enter your Service/Item offer");
$errormsg;
}
$nameowner = clean($_POST['name-owner']);
if($nameowner == NULL)
{
die ("Please enter the Name of Owner/Manager");
$errormsg;
}
$namerep = clean($_POST['name-rep']);
if($namerep == NULL)
{
die ("Please enter the Name of Representative");
$errormsg;
}
$contract_from = clean($_POST['contract-from']);
if($contract_from == NULL)
{
die ("Please enter the Contract date");
$errormsg;
}
$contract_to = clean($_POST['contract-to']);
if($contract_to == NULL)
{
die ("Please enter the Contract date");
$errormsg;
}
$agree = clean($_POST['agree']);
$message = "Business Name: ".$name."\r\n";
$message = "Business Address: " .$address. "\r\n";
$message = "Phone No.: ".$hp."\r\n";
$message = "Email: ".$email."\r\n";
$message = "URL: <a href=\"".$url."\"\r\n ";
$message = "Amount/Percentage offer: ".$rm."\r\n";
$message = "Items/Service offer:".$service."\r\n";
$message = "Name of Owner/Manager".$nameowner."\r\n";
$message = "Name of Representative".$namerep."\r\n";
$message = "This contract is valid for one(1) year from: <b>".$contract_from."</b> to <b>".$contract_to."</b> \r\n";
$message = $agree."\r\n";
$to = 'contact#example.com';
$subject = 'Contact query';
$headers = 'From:' . "$email \r\n" .
'Reply-To:' . "$email \r\n" .
'X-Mailer: PHP/' . phpversion();
$send = mail($to, $subject, $message, $headers);
if ($send == TRUE)
{
echo "<p>Message has been sent</p>";
echo "<p>Thank you</p>";
}
else
{
die ("<p>Message failed to send</p>");
}
}else {
echo "<p>Oops.. There seems to be a technical error<br>We are truly sorry, please check again later</p>";
}
?>
Probably one of the POST Variable is not set and thus it might throw you to the else part to show the error
You must attach the require_once "Mail.php"; require_once('Mail/mime.php'); into your code so that the function works for you
<?php
function clean($str)
{
$str = mysql_real_escape_string($str);
$str = htmlspecialchars($str);
$str = strip_tags($str);
return($str);
}
if(isset($_POST['submit']))
{
$name = clean($_POST['name']);
if($name == NULL || $name == "")
{
die ("Please enter your name");
$errormsg = "<button class=\"\" onClick=\"history.go(-1)\"> Retry</button> ";
}
$address = clean($_POST['address']);
if($address == NULL)
{
die ("Please enter your Business address");
$errormsg;
}
$hp = clean($_POST['hp']);
if($hp == NULL)
{
die ("Please enter your Phone No.");
}
$email = clean($_POST['email']);
if($email == NULL)
{
die ("Please enter your Email address");
$errormsg;
}
$url = clean($_POST['url']);
$rm = clean($_POST['rm']);
if($rm == NULL)
{
die ("Please enter your Amount/Percentage");
$errormsg;
}
$service = clean($_POST['service']);
if($service == NULL)
{
die ("Please enter your Service/Item offer");
$errormsg;
}
$nameowner = clean($_POST['name-owner']);
if($nameowner == NULL)
{
die ("Please enter the Name of Owner/Manager");
$errormsg;
}
$namerep = clean($_POST['name-rep']);
if($namerep == NULL)
{
die ("Please enter the Name of Representative");
$errormsg;
}
$contract_from = clean($_POST['contract-from']);
if($contract_from == NULL)
{
die ("Please enter the Contract date");
$errormsg;
}
$contract_to = clean($_POST['contract-to']);
if($contract_to == NULL)
{
die ("Please enter the Contract date");
$errormsg;
}
$agree = clean($_POST['agree']);
$message = "Business Name: ".$name."\r\n";
$message = "Business Address: " .$address. "\r\n";
$message = "Phone No.: ".$hp."\r\n";
$message = "Email: ".$email."\r\n";
$message = "URL: <a href=\"".$url."\"\r\n ";
$message = "Amount/Percentage offer: ".$rm."\r\n";
$message = "Items/Service offer:".$service."\r\n";
$message = "Name of Owner/Manager".$nameowner."\r\n";
$message = "Name of Representative".$namerep."\r\n";
$message = "This contract is valid for one(1) year from: <b>".$contract_from."</b> to <b>".$contract_to."</b> \r\n";
$message = $agree."\r\n";
$to = 'contact#example.com';
$subject = 'Contact query';
$headers = 'From:' . "$email \r\n" .
'Reply-To:' . "$email \r\n" .
'X-Mailer: PHP/' . phpversion();
$send = mail($to, $subject, $message, $headers);
if ($send == TRUE)
{
echo "<p>Message has been sent</p>";
echo "<p>Thank you</p>";
}
else
{
die ("<p>Message failed to send</p>");
}
}
else
{
echo "<p>Oops.. There seems to be a technical error<br>We are truly sorry, please check again later</p>";
}
?>
I have a jQuery AJAX form and I'd like for it to do better validation on input - on the PHP side. Here is the PHP script that works with AJAX:
while (true) {
if (empty($_POST['name'])) {
$return['error'] = true;
$return['msg'] = 'You did not enter your name.';
break;
}
if (empty($_POST['email'])) {
$return['error'] = true;
$return['msg'] = 'You did not enter your email.';
break;
}
if (empty($_POST['message'])) {
$return['error'] = true;
$return['msg'] = 'You did not enter your message.';
break;
}
break;
}
if (!$return['error'])
$return['msg'] = "Thank you: {$_POST['name']}<br />email address: {$_POST['email']}<br />Your Message: {$_POST['message']}";
echo json_encode($return);
And here is the PHP script I use elsewhere for validation:
<?php
$subject = "Website Contact Form Enquiry";
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'info#bgv.co.za'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
Please can someone who know their PHP help me - I dont have a clue how to put this together.
try like this. i think this code alone working good for what you want.
$subject = "Website Contact Form Enquiry";
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
$return['msg'] = 'You did not enter your name.';
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
$return['msg'] = 'You did not enter your email.';
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
$return['msg'] = 'You did not enter valid email.';
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
$return['msg'] = 'You did not enter your message.';
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'info#bgv.co.za'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}else{
echo json_encode($return);
}
}