Php Web form Message Gone After Error - php

my web form work fine, except
if user submit the form with 1 or more field missing
user will see error message and all the input/message gone.
user will have to type the message again
1) How to keep the message so user don't have to type again?
2) is my web form secure from spammer?
my web form
http://www.jewelryindonesia.com/contact.php
my form code:
<?php
// check for a successful form post
if (isset($_GET['s'])) echo "<div class=\"alert alert-success\">".$_GET['s']."</div>";
// check for a form error
elseif (isset($_GET['e'])) echo "<div class=\"alert alert-danger\">".$_GET['e']."</div>";
?>
<form role="form" method="POST" action="contact-form-submission.php">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1">
</div>
<div class="form-group col-lg-4">
<label for="input2">Email Address</label>
<input type="email" name="contact_email" class="form-control" id="input2">
</div>
<div class="form-group col-lg-4">
<label for="input3">Phone Number/WhatsApp</label>
<input type="phone" name="contact_phone" class="form-control" id="input3">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4"></textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
my php:
<?php
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
$headers = "From: $email_address\r\n";
$headers .= "Reply-To: $email_address\r\n";
// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone Number: $phone\n";
$email_content .= "Message:\n\n$message";
// send the email
//ENTER YOUR INFORMATION BELOW FOR THE FORM TO WORK!
mail ('xxxx#gmail.com', 'Jewelryxxx.com Online Form', $email_content, $headers);
// send the user back to the form
header('Location:jewelry-indonesia-thankyou.html?s='.urlencode('Your Message Has Been Sent ! Thank you for your message.')); exit;
?>

To avoid user don't have to type again when your script jump into error validation, you can store $_POST field to a variable, then call again that variable on html field value.
Web form spammer can be avoided by placing capcha and more validation input, as mentioned above simply can be ussing isset.
isset($_POST['keterangan']) for example
!isset() and $_POST[var]==""; same meaning
EDIT all proccess on 1 page code contact.php
<?php
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
if ($name=="" || $email_address="" || $phone=="" || $message==""){
if ($_POST['save']=="contact"){
echo "semua field harus diisi untuk melanjutkan";
}
}else{
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (!isset($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
echo "<div class=\"alert alert-danger\">".$error."</div>";
}else{
$headers = "From: $email_address\r\n";
$headers .= "Reply-To: $email_address\r\n";
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone Number: $phone\n";
$email_content .= "Message:\n\n$message";
mail ('xxxx#gmail.com', 'Jewelryxxx.com Online Form', $email_content, $headers);
// send the user back to the form
echo "<div class=\"alert alert-success\">Your Message Has Been Sent ! Thank you for your message.</div>";
}
}
echo '<form role="form" method="POST" action="contact.php">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1" value="'.$name.'">
</div>
<div class="form-group col-lg-4">
<label for="input2">Email Address</label>
<input type="email" name="contact_email" class="form-control" id="input2" value="'.$email_address.'">
</div>
<div class="form-group col-lg-4">
<label for="input3">Phone Number/WhatsApp</label>
<input type="phone" name="contact_phone" class="form-control" id="input3" value="'.$phone.'">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4">'.$message.'</textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>';
?>

Related

How to get an email from a filled form using PHP script? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 1 year ago.
I'm trying to create a php script to send a filled form from a website to a specific email. I tried several ways but I don't get the email on my webmail. I also don't get any errors so I am not sure where the problem is. The files are in the same directory and the webmail seems to work well. I would be grateful for your input.
This is email.php file:
<?php
$error = ""; $successMessage = "";
if ($_POST) {
if (!$_POST["name"]) {
$error .= "A name is required.<br>";
}
if (!$_POST["email"]) {
$error .= "An email address is required.<br>";
}
if (!$_POST["choice"]) {
$error .= "Select a trial session.<br>";
}
if (!$_POST["date"]) {
$error .= "Select a date.<br>";
}
if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "The email address is invalid.<br>";
}
if ($error != "") {
$error = '<div class="alert alert-danger" role="alert"><p>There were error(s) in your form:</p>' . $error . '</div>';
} else {
$emailTo = "my#addressemail.com";
$subject = "trial session";
$name = $_POST['name'];
$choice = $_POST['choice'];
$date = $_POST['date'];
$headers = "From: ".$_POST['email'];
if (mail($emailTo, $subject, $name, $choice, $date, $headers)) {
$successMessage = '<div class="alert alert-success" role="alert">Your message was sent, we\'ll get back to you ASAP!</div>';
} else {
$error = '<div class="alert alert-danger" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';
}
}
}
?>
<div class="container">
<h1>Get in touch!</h1>
<div id="error"><? echo $error.$successMessage; ?></div>
<form method="post" action=”email.php”>
<fieldset class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" >
</fieldset>
<fieldset class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
<small class="text-muted">We'll never share your email with anyone else.</small>
</fieldset>
<div class="mb-3">
<div class="control-label" id="choice" name="choice">Which trial session you would like to book?</div>
<select class="form-select" required aria-label="select example" id="choice" name="choice">
<option value="">I'm interested in...</option>
<option value="1">option1</option>
<option value="2">option2</option>
</select>
</div>
<div class="form-group"> <!-- Date input -->
<label class="control-label" for="date">Date</label>
<input class="form-control" id="date" name="date" placeholder="MM/DD/YYY" type="text"/>
</div>
<button type="submit" id="submit" class="btn btn-primary">Submit</button>
</form>
</div>
By default, the server cannot send an email. You have to set up for it an mail server. And as my experience, most of the time we must use the SMTP server to send the email.
You can check the way to send email via SMTP in PHP here:
Sending email with PHP from an SMTP server
You also have to set up your email by allow less secure apps:
https://kb.synology.com/en-global/SRM/tutorial/How_to_use_Gmail_SMTP_server_to_send_emails_for_SRM

PHP form fills with ones after submission

I am stumped....
I have created a PHP form to gather some user information and email it to the site owner, after the form is submitted, the fields auto complete with a '1' I am assuming this means the field was true and submitted.
Does anyone have any suggestions on how to hide or remove the 1 after the form is submitted? I just want the form to be blank after submission. Here is the code, Sorry for the poor formatting and eye bleeding wall of code.
Thank you in advance for any help!
PHP Code
$errName = "";
$errcatBreed = "";
$errEmail = "";
$errMessage = "";
$result = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$catBreed = $_POST['catBreed'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'PearTreeHill Contact Form';
$to = 'test#domain.com';
$subject = "New furbaby enquiry from $name";
$body ="From: $name\n Cat Breed: $catBreed\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if cat breed has been entered
if (!$_POST['catBreed']) {
$errcatBreed = 'Please enter either Ragdoll or British ShortHair';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// RECAPTCHA
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
$secret = 'Private Key';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success)
{
$succMsg = 'Your contact request have submitted successfully.';
}
else
{
$errMsg = 'Robot verification failed, please try again.';
}
}
// If there are no errors, send the email
if (!$errName && !$errcatBreed && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
HTML Code
<!-- contact form -->
<section id="contactUs" class="bg">
<div class="container">
<div class="row">
<div class="col-lg-8 mx-auto">
<div class="text-center">
<h2 class="w3-tangerine">Contact Us</h2>
</div>
<!-- start of entry form -->
<form class="form-horizontal" role="form" method="post" action="index.php">
<!-- name entry -->
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars(isset($_POST['name'])); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<!-- cat breed selection -->
<div class="form-group">
<label for="catBreed" class="col-sm-2 control-label">Cat Breed</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="catBreed" name="catBreed" placeholder="'Ragdoll' or 'British ShortHair'" value="<?php echo htmlspecialchars(isset($_POST['catBreed'])); ?>">
<?php echo "<p class='text-danger'>$errcatBreed</p>";?>
</div>
</div>
<!-- email address entry -->
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-12">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars(isset($_POST['email'])); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<!-- Body of message -->
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-12">
<textarea class="form-control" rows="4" name="message" placeholder="Please enter any other information"><?php echo htmlspecialchars(isset($_POST['message']));?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- reCAPTCHA -->
<div class="g-recaptcha" data-sitekey="Public Key" data-callback="recaptcha_callback"></div>
<!-- send button -->
<input disabled="disabled" id="submit" name="submit" type="submit" value="Send Message" class="btn btn-danger">
<label for="submit" class="col-sm-8 control-label">Please allow up to 48 hours for a response!</label>
</div>
</div>
<!-- entry alert -->
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
<?php echo htmlspecialchars(isset($_POST['email'])); ?>
Here you display the result of isset() (TRUE or FALSE).
You should do :
<?php if ( isset($_POST['email']) ) echo htmlspecialchars($_POST['email']); ?>
And this for each field :-)
change
echo htmlspecialchars(isset($_POST['name']));
to
echo htmlspecialchars ($_POST['name'])
I mean remove isset();

form submission error html & php

I am buidling a php contact form with ajax validation. While I submit the contact form I get error message "spammer" which is not supposed to.
Below is my code html & php code.
Index.php
<form id="contactform" method="post" action="mailer.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="username" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Enter your phone number">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email address">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" rows="4" placeholder="Enter your email message"></textarea>
</div>
<br>
<div class="g-recaptcha" data-sitekey="6Lct9zUUAAAAABpj9vwKX9B7x_AH8s9gwJzFW1sB"></div>
<br>
<div id="formresult">
</div>
<button type="submit" name="submit" class="btn btn-info">Submit</button>
</form>
Mailer.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["username"]);
$phone = trim($_POST["phone"]);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
if(isset($_POST['g-recaptcha-response'])){
$captcha = $_POST['g-recaptcha-response'];
}
//Validate the data
if (empty($name) OR empty($phone) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($captcha)) {
http_response_code(400);
echo "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span> <strong>Please fill all the form inputs and check the captcha to submit.</strong>";
exit;
}
//recipient email address.
$recipient = "abc#abc.com";
$subject = "New message from $name";
//email content.
$email_content = "Name: $name\n";
$email_content .= "Phone: $phone\n\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
//email headers
$email_headers = "From: $name <$email>";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRATE_KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$decoded_response = json_decode($response, true);
if($decoded_response['success'] == true){
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span> <strong>Thank You! Your message has been sent.</strong>";
} else {
http_response_code(500);
echo "Your message cannot be sent";
}
} else {
http_response_code(400);
echo 'spammer!';
}
}
?>
When I submit the form I get a error message 'spammer' and even mail does not get through.
I know there is some mistake in logic but i cannot figure it out.
Anyone please help me in this. Thank you.
The line of code:
if($decoded_response['success'] == true){
Must be evaluating to false, and executing the else part of that statement. Displaying the spammer message.
Check the decoded contents returned from the recaptcha url, that you assign to the $decoded_response variable.
print_r($decoded_response); // should show the whole contents

Receiving Empty email from Submit form every day around the same time

I am receiving empty email from Submit form every day around the same time.
Form Code :
<div class="form">
<h4 class="h-light text-center">Submit a Query</h4>
<form class="form-horizontal" action="sendemail.php" method="post" id="contact_form">
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input name="name" placeholder="Name" class="form-control input-text" type="text" required>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input name="email" placeholder="E-Mail Address" class="form-control input-text" type="email" required>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label">Phone #</label>
<div class="col-sm-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
<input name="phone" placeholder="(123)456-7890" class="form-control input-text" type="text" required>
</div>
</div>
</div>
<!-- Text area -->
<div class="form-group">
<label class="col-sm-2 control-label">Message</label>
<div class="col-sm-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control input-text text-area" name="message" placeholder="Enter your massage for us here. We will get back to you." required></textarea>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"><img src="media/home/captcha.png"></label>
<div class="col-sm-10">
<div class="input-group">
<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"></div>
</div>
</div>
</div>
<!-- Success message -->
<div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> Thanks for contacting us, we will get back to you shortly.</div>
<!-- Button -->
<div class="form-group">
<label class="col-sm-4 control-label"></label>
<div class="col-sm-8">
<button type="submit" class="input-btn" >Send <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</form>
sendemail.php Code:
<?php
/* These are the variable that tell the subject of the email and where the email will be sent.*/
$emailSubject = 'New query !';
$mailto = 'xxx#domain.in';
/* These will gather what the user has typed into the field. */
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
/* This takes the information and lines it up the way you want it to be sent in the email. */
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email Address: $email <br>
Phone Number: $phone <br>
Message: $message<br>
EOD;
$headers = "From:domain.in<xxx#domain.in>\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
//$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
if(mail($mailto, $emailSubject, $body, $headers))
{
echo "Your query has been submitted successfully. ";
echo 'Go Back';
}
else
{
echo "Failure";
}
?>
I have no idea what is going on, the code seems to be fine, but still i am receiving that empty email only with the field name like...
Name :
Email Address :
Phone Number :
Message :
But no information filled in it.
Thanks in advance.
Validate the input on the PHP side before you send the email. eg:
$valid = true;
$name = $_POST['name'];
if(empty($name))
{
echo('Please enter your name');
$valid = false;
}
// Repeat for other fields
if($valid)
{
// Send the email
}
I am not so good with PHP, please let me know if it correct.. Thank you
<?php
/* These are the variable that tell the subject of the email and where the email will be sent.*/
$emailSubject = 'New query!';
$mailto = 'xxx#domain.in';
/* These will gather what the user has typed into the fieled. */
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
/* This takes the information and lines it up the way you want it to be sent in the email. */
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email Address: $email <br>
Phone Number: $phone <br>
Message: $message<br>
EOD;
$headers = "From:domain.in<info#domain.in>\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
//$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
$valid = true;
$name = $_POST['name'];
if(empty($name))
{
echo('Please enter your name');
$valid = false;
}
$email = $_POST['email'];
if(empty($email))
{
echo('Please enter your email');
$valid = false;
}
$phone = $_POST['phone'];
if(empty($phone))
{
echo('Please enter your Phone number');
$valid = false;
}
$message = $_POST['message'];
if(empty($message))
{
echo('Please enter your message');
$valid = false;
}
if($valid)
{
if(mail($mailto, $emailSubject, $body, $headers))
{
echo "Your query has been submitted successfully. ";
echo 'Go Back';
}
}
else
{
echo "Failure";
}
i
?>
Should be working now?
$valid = true;
$name = $_POST['name'];
if(empty($name))
{
echo('Please enter your name');
$valid = false;
}
<br/>
$email = $_POST['email'];
if(empty($email))
{
echo('Please enter your email');
$valid = false;
}
<br/>
$phone = $_POST['phone'];
if(empty($phone))
{
echo('Please enter your Phone number');
$valid = false;
}
<br/>
$message = $_POST['message'];
if(empty($message))
{
echo('Please enter your message');
$valid = false;
}
<br/>
if($valid)
{
if(mail($mailto, $emailSubject, $body, $headers))
{
echo "Your query has been submitted successfully. ";
echo 'Go Back';
}
}
else
{
echo "Failure";
}
?>
Checking for whether $_Post is empty is the usual and logical way to go.
But for someone who is curious about why the same empty email is being sent to you everyday (usually it's some kind of bot) why not add the following fields in your message body so you know what's happening.
This way you learn how the web works better too.
For example
$ip = $_SERVER['REMOTE_ADDR']; // for IP address
$browser = $_SERVER['HTTP_USER_AGENT']; // get browser info
and you add above info to your message body so you know what's going on.

PHP Change name of sender. [duplicate]

This question already has an answer here:
change sender email address in php form to recipient
(1 answer)
Closed 7 years ago.
When the form is submitted I would like to change the name of the sender. Right now it just shows my test domain name as the sender when I get the email.
<?php
if ($_POST["submit"]) {
$result='<div class="alert alert-success">Form submitted</div>';
if (!$_POST['name']) {
$error="<br />Please enter your name";
}
if (!$_POST['email']) {
$error.="<br />Please enter your email address";
}
if (!$_POST['comment']) {
$error.="<br />Please enter a comment";
}
if ($_POST['email']!="" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error.="<br />Please enter a valid email address";
}
if ($error) {
$result='<div class="alert alert-danger"><strong>There were error(s) in your form:</strong>'.$error.'</div>';
} else {
if (mail("email#email.com", "Comment from website!", "Name: ".$_POST['name'],."
Email: ".$_POST['email']."
Comment: ".$_POST['comment'])) {
$result='<div class="alert alert-success"><strong>Thank you!</strong> I\'ll be in touch</div>';
} else {
$result='<div class="alert alert-danger"><strong>Sorry, there was an error sending your message. Please try again later.</strong></div>';
}
}
}
?>
Form
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 emailForm">
<h1>My email form</h1>
<?php echo $result; ?>
<p class="lead"> Please get in touch - I'll get back to you as soon as I can.</p>
<form method="post">
<div class="form-group">
<label for="name">Your Name:</label>
<input type="text" name="name" class="form-control" placeholder="Your Name" value="<?php echo $_POST['name']; ?>" />
</div>
<div class="form-group">
<label for="email">Your Email:</label>
<input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo $_POST['email']; ?>" />
</div>
<div class="form-group">
<label for="comment">Your Comment:</label>
<textarea class="form-control" name="comment"><?php echo $_POST['comment']; ?></textarea>
</div>
<input type="submit" name="submit" class="btn btn-success btn-lg" value="Submit" />
</form>
</div>
</div>
</div>
This is my first contact form, and would really appreciate the help!
Thanks in advance!
As stated in the PHP Documentation
$to = 'user#example.com';
$subject = 'Comment from website!';
$message = 'hello';
$headers = 'From: your_name#example.com' . "\r\n" .
'Reply-To: your_name#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

Categories