PHP email contact form displays error - php

Every time I attempt to submit this contact form I receive the following error message:
'Please enter your message.'
The name error message and email error message do not appear unless I leave them blank. I attempted specifying post in the HTML.
Here is the HTML:
<div class="col-md-8 animated fadeInLeft notransition">
<h1 class="smalltitle">
<span>Get in Touch</span>
</h1>
<form action="contact.php" method="post" name="MYFORM" id="MYFORM">
<input name="name" size="30" type="text" id="name" class="col-md-6 leftradius" placeholder="Your Name">
<input name="email" size="30" type="text" id="email" class="col-md-6 rightradius" placeholder="E-mail Address">
<textarea id="message" name="message" class="col-md-12 allradius" placeholder="Message" rows="9"></textarea>
<img src="contact/refresh.jpg" width="25" alt="" id="refresh"/><img src="contact/get_captcha.php" alt="" id="captcha"/>
<br/><input name="code" type="text" id="code" placeholder="Enter Captcha" class="top10">
<br/>
<input value="Send" type="submit" id="Send" class="btn btn-default btn-md">
</form>
</div>
Here is the PHP:
<?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your message.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - replace your email here
$to = 'faasdfsdfs#gmail.com';
//sender - from the form
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Message from ' . $name;
$message = 'Name: ' . $name . '<br/><br/>
Email: ' . $email . '<br/><br/>
Message: ' . nl2br($comment) . '<br/>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo 'Back';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}

In your HTML form you name your <input... field "message" but then when you are in PHP you try to get the value from `$_GET['comment'].
I think if you get those lined up I think it will solve your problem.

I can see 2 issues:
Receive $_GET['comment'] or $_POST['comment'] but next you're using $message var
Do not use if($_POST) because $_POST as a superglobal
is always setted.
I suggest use this for check if a POST have been sent
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {}
or this in case you want to check is not empty
if ( !empty($_POST) ) {}
How to detect if $_POST is set?

Related

How to integrate reCAPTCHA with a form

I can't work out how to add Google reCAPTCHA to this contact form. I have no problems adding on the front end but can't seem to apply to the server side.
<!-- Form -->
<div id="contact-form">
<form method="post" action="contact.php">
<div class="field">
<label>Name:</label>
<input type="text" name="name" class="text" />
</div>
<div class="field">
<label>Email: <span>*</span></label>
<input type="text" name="email" class="text" />
</div>
<div class="field">
<label>Message: <span>*</span></label>
<textarea name="message" class="text textarea" ></textarea>
</div>
<div class="field">
<input type="button" class="button light medium" id="send" value="Send Message"/>
</div>
<div class="field">
<input type="button" class="button gray medium" value="Reset!"/>
</div>
<div class="loading"></div>
</form>
</div>
PHP Server Side
<?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$message) $errors[count($errors)] = 'Please enter your message.';
//If the errors array is empty, send the mail
if (!$errors) {
// ====== Your mail here ====== //
$to = 'admin#mysite.com <admin#mysite.com>';
// Sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Contact Message from the Byblos Group Website';
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name:</td><td>' . $name . '</td></tr>
<tr><td>Email:</td><td>' . $email . '</td></tr>
<tr><td>Message:</td><td>' . nl2br($message) . '</td></tr>
</table>
</body>
</html>';
// Send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
// If the errors array has values
} else {}
// Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>
We are validating google reCAPTCHA like this
$fileContent = '';
if (isset($_REQUEST['g-recaptcha-response']) && !empty($_REQUEST['g-recaptcha-response'])) {
$fileContent = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=your_recaptcha_secret_key&response=". $_REQUEST['g-recaptcha-response']);
}
$jsonArray = json_decode($fileContent, true);
if (isset($jsonArray['success']) && $jsonArray['success']==true) {
// process your logic here
} else {
echo 'Invalid verification code, please try again!';
}
I'm actually using this self-made function to check if reCAPTCHA is valid on my website
function checkCaptcha($cResp) {
$captchaSecret = "yourSecret";
$captchaRequestUrl = 'https://www.google.com/recaptcha/api/siteverify?secret='.$captchaSecret.'&response='.$cResp;
$captchaResponse = #file_get_contents($captchaRequestUrl);
if (!$captchaResponse) {
return 2;
}
$captchaResponse = json_decode($captchaResponse);
$captchaSuccess = $captchaResponse->{'success'};
if (!$captchaSuccess) {
return 3;
}
return 1;
}
Then you can check like the following
if (checkCaptcha($_POST['g-recaptcha-response']) === 1) // success
if (checkCaptcha($_POST['g-recaptcha-response']) === 2) // no resp
if (checkCaptcha($_POST['g-recaptcha-response']) === 3) // fail

sendmail.php not re-directing

I had/have the code below for users to send messages/emails from my website to my sites email address.
It was working fine, for weeks: Validating the field contents (if there was a blank 'required field', it asked for a "valid" field content i.e. email address), as long as there were no blank fields it was sending the email and was re-directing to the thank-you page - which acknowledges that the email has been sent.
Now, it has stopped working properly.
It doesn't seem to be validating (as it did originally) any more - as there are no warnings/errors for blank fields (if there is a blank field it simply doesn't send), it still sends the email to the address correctly (if there are no blank required fields), but it doesn't redirect to the thank-you page any more.
Here is the form code:
<form method="post" action="assets/sendmail.php">
<label for="name" class="nameLabel">Name</label>
<input id="name" type="text" name="name" placeholder="Enter your name...">
<label for="email" class="emailLabel">Email</label>
<input id="email" type="text" name="email" placeholder="Enter your email...">
<label for="subject">Subject</label>
<input id="subject" type="text" name="subject" placeholder="Your subject...">
<label for="message" class="messageLabel">Message</label>
<textarea id="message" name="message" placeholder="Your message..."></textarea>
<button type="submit">Send</button>
</form>
Here is the php code - note that I have substituted the email address for obvious reasons :)
<?php
// Email address verification
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email));
}
if($_POST) {
// Enter the email where you want to receive the message
$emailTo = 'me#myemail.com';
$clientName = trim($_POST['name']);
$clientEmail = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
$array = array();
$array['nameMessage'] = '';
$array['emailMessage'] = '';
$array['messageMessage'] = '';
if($clientName == '') {
$array['nameMessage'] = 'Please enter your name.';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Please insert a valid email address.';
}
if($message == '') {
$array['messageMessage'] = 'Please enter your message.';
}
if($clientName != '' && isEmail($clientEmail) && $message != '') {
// Send email
$headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
mail($emailTo, $subject, $message, $headers);
}
//echo json_encode($array);
{
header("location:../thankyou.html");
}
}
?>
Any help would be appreciated.
First of all, for email checking you can use built-in php as well.
filter_var($clientEmail, FILTER_VALIDATE_EMAIL)
I don't see where you output your errors so that may be why they are not showing,
I have added support for that
I have rewritten your code:
<?php
if (!empty($_POST)) {
// Enter the email where you want to receive the message
$emailTo = 'me#myemail.com';
$clientName = trim($_POST['name']);
$clientEmail = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
$errors = array();
if (empty($clientName)) {
$errors['nameMessage'] = 'Please enter your name.';
}
if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) {
$errors['emailMessage'] = 'Please insert a valid email address.';
}
if (empty($message)) {
$errors['messageMessage'] = 'Please enter your message.';
}
// Are there errors?
if (count($errors) == 0) {
// Send email
$headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
mail($emailTo, $subject, $message, $headers);
header("location:../thankyou.html");
} else {
foreach ($errors as $err) {
echo $err . '<br />';
}
}
}
?>
Thank-you for the updated PHP code. Unfortunately, it hasn't made any difference. It still sends the email when all fields are filled in, it even sends if the "subject" is empty, but it still does not re-direct to the thank-you page. It doesn't send if Name, Email Address, and Message are empty. But it does not display errors. I have tried to place both files in the parent directory and removing the assets/ from the action in the form and the ../ in the PHP. as below
<form method="post" action="sendmail.php">
<label for="name" class="nameLabel">Name</label>
<input id="name" type="text" name="name" placeholder="Enter your name...">
<label for="email" class="emailLabel">Email</label>
<input id="email" type="text" name="email" placeholder="Enter your email...">
<label for="subject">Subject</label>
<input id="subject" type="text" name="subject" placeholder="Your subject...">
<label for="message" class="messageLabel">Message</label>
<textarea id="message" name="message" placeholder="Your message..."></textarea>
<button type="submit">Send</button>
</form>
And the PHP is as you supplied (changing the email address of course)
<?php
if (!empty($_POST)) {
// Enter the email where you want to receive the message
$emailTo = 'me#myemail.com';
$clientName = trim($_POST['name']);
$clientEmail = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
$errors = array();
if (empty($clientName)) {
$errors['nameMessage'] = 'Please enter your name.';
}
if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) {
$errors['emailMessage'] = 'Please insert a valid email address.';
}
if (empty($message)) {
$errors['messageMessage'] = 'Please enter your message.';
}
// Are there errors?
if (count($errors) == 0) {
// Send email
$headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
mail($emailTo, $subject, $message, $headers);
header("location:thankyou.html");
} else {
foreach ($errors as $err) {
echo $err . '<br />';
}
}
}
?>
No Errors displayed if there are errors, no re-direct once sent. It just remains on the "contact" page and still displaying the completed form. Cannot understand why the re-direct was working but not now. Couldn't sign in when I posted this originally.

I am attempting to create a working PHP contact form

At the bottom of this page under the section 'Quick Message' in the lower right there is a a contact form I'm trying to fix.
http://danielgruttadaro.chapter7ny.com
The form does not allow submissions that do not have all three sections filled out. However, I am receiving the error: 'sorry unexpected error please try again later' when the form is properly filled out. Below is the relevant html and php. Thanks for your help.
<form method="post" action="contact.php" id="contactform">
<div class="form">
<input class="col-md-6" type="text" name="name" placeholder="Name">
<input class="col-md-6" type="text" name="email" placeholder="E-mail">
<textarea class="col-md-12" name="message" rows="4" placeholder="Message"></textarea>
<input type="submit" id="submit" class="btn" value="Send">
</div>
Here is the php:
<?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$message) $errors[count($errors)] = 'Please enter your message.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - replace your email here
$to = 'dan#chapter7ny.com';
//sender - from the form
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Message from ' . $name;
$message = 'Name: ' . $name . '<br/><br/>
Email: ' . $email . '<br/><br/>
Message: ' . nl2br($message) . '<br/>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo 'Back';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>
Most likely the machine that is running php doesn't have a mail server set up as well. The mail function requires that. You can use a library like:
https://github.com/PHPMailer/PHPMailer
and use an existing gmail account to send your email:
http://phpmailer.worxware.com/?pg=examplebgmail
which will let you mail yourself whatever they type in. This is what I use in almost all of my projects.
You can also use PEAR:
https://pear.php.net/package/Mail
Which I have also had luck with in the past. Godspeed.

Sending mail in PHP results in undefined index

I am trying to get my contact form on my site to operate correctly. I am getting undefined index in regards to the $name, $email, $thesubject, $message variables, respectively.
Could anyone tell me what I need to do to get the email to properly send?
HTML/Form:
<div class="alert success success-message">
<div class="close">×</div>
<p>Your message has been sent!</p>
</div>
<form class="clearfix" method="post" action="contact.php">
<div class="field">
<label>Name <span>*</span></label>
<input type="text" name="name" class="text" value="" />
</div>
<div class="field">
<label>Email <span>*</span></label>
<input type="email" name="email" class="text" value="" />
</div>
<div class="field field-last">
<label>Subject <span>*</span></label>
<input type="text" name="thesubject" class="text" value="" />
</div>
<textarea name="message" class="text"></textarea>
<button id="send" class="btn">Submit</button>
<div class="loading"></div>
</form>
</div>
PHP:
<?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$thesubject = ($_GET['thesubject']) ?$_GET['thesubject'] : $_POST['thesubject'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$thesubject) $errors[count($errors)] = 'Please enter your subject.';
if (!$message) $errors[count($errors)] = 'Please enter your message.';
//if the errors array is empty, send the mail
if (!$errors) {
// ====== Your mail here ====== //
$to = 'myemail#gmail.com';
//sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'yourwebsite.com / ' . $thesubject . '';
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name:</td><td>' . $name . '</td></tr>
<tr><td>Email:</td><td>' . $email . '</td></tr>
<tr><td>Message:</td><td>' . nl2br($message) . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>
use isset function to check if variable is setup. For example:
$name = isset($_GET['name']) ? $_GET['name'] : $_GET['name'];

PHP contact form with HTML5 validation, display a message?

I have a working php contact form, with HTML5 validation for inputs ( added required to them, and type name/email), but I have one problem.
when message is sent it opens a new tab and it says thank you your message was sent, which is normal because of process.php
<?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$website = ($_GET['website']) ?$_GET['website'] : $_POST['website'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course,
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your message.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'youremail#email.com';
//sender
$from = $email;
//subject and the html message
$subject = 'Hello from ' . $name;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name: </td><td>' . $name . '</td></tr>
<tr><td>Email: </td><td>' . $email . '</td></tr>
<tr><td>Message: </td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo 'Back';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>
but how can I make it show the message under submit button?
this is HTML side
<form method="post" action="process.php">
<div class="element">
<label><i class="icon-user"></i></label>
<input type="name" name="name" placeholder="write your name here" class="text" required/>
</div>
<br>
<div class="element">
<label><i class="icon-envelope"></i></label>
<input type="email" name="email" placeholder="write your e-mail here" class="text" required/>
</div>
<br>
<div class="element">
<label><i class="icon-comment"></i></label>
<textarea name="comment" placeholder="write your message here" class="text textarea"required></textarea>
</div>
<br>
<div class="element el-submit">
<input class="submit" type="submit" id="submit"/>
</div>
</form>
I mean, when I press submit button and it's alright I want to display a message like "your message was sent", but I want it under submit button, not in a new page
Make the file into one so that you POST data to the same file as below
<?php
if($_POST) //If the form is submitted
{
$notification=""; //Used for catching all your messages
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$website = ($_GET['website']) ?$_GET['website'] : $_POST['website'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course,
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your message.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'youremail#email.com';
//sender
$from = $email;
//subject and the html message
$subject = 'Hello from ' . $name;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name: </td><td>' . $name . '</td></tr>
<tr><td>Email: </td><td>' . $email . '</td></tr>
<tr><td>Message: </td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else $notification.= 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
$notification.= $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
$notification.= 'Back';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
} //First If loop
?>
<form method="post" action="process.php">
<div class="element">
<label><i class="icon-user"></i></label>
<input type="name" name="name" placeholder="write your name here" class="text" required/>
</div>
<br>
<div class="element">
<label><i class="icon-envelope"></i></label>
<input type="email" name="email" placeholder="write your e-mail here" class="text" required/>
</div>
<br>
<div class="element">
<label><i class="icon-comment"></i></label>
<textarea name="comment" placeholder="write your message here" class="text textarea"required></textarea>
</div>
<br>
<div class="element el-submit">
<input class="submit" type="submit" id="submit"/>
<?php
if(!empty($notification)) //This will display notification after submit
{
echo $notification;
}
?>
</div>
</form>
You can also make use of AJAX to achieve what you required.
The following tutorials are the best samples.
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
http://www.9lessons.info/2009/04/submit-form-jquery-and-ajax.html
http://www.phpeveryday.com/articles/jQuery-AJAX-Form-Submission-P973.html
If you don't want to show that on next page create pop up for show the Thank you message its look great.
use your html code in this way.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
function myFunction()
{
alert("Thanks for mail!");
}
</script>
</head>
<body>
<form method="post" action="process.php">
<div class="element">
<label><i class="icon-user"></i></label>
<input type="name" name="name" placeholder="write your name here" class="text" required/>
</div>
<br>
<div class="element">
<label><i class="icon-envelope"></i></label>
<input type="email" name="email" placeholder="write your e-mail here" class="text" required/>
</div>
<br>
<div class="element">
<label><i class="icon-comment"></i></label>
<textarea name="comment" placeholder="write your message here" class="text textarea"required></textarea>
</div>
<br>
<div class="element el-submit">
<input type="button" onclick="myFunction()" value="Submit">
</div>
</form>
</body>
</html>
important point missed in replies so far is the action="some.php" that should be changed to action="" should you delete the server-side PHP; hence, action of "post" is inside the contact form. I found a comprehensive tutorial on this subject relating HTML5 CSS3 and PHP here.

Categories