Contact Form: Why won't this form validate? - php

I have a contact form at the bottom of a, (single page business/portfolio), website.
The script above my DOCTYPE looks like this.
<?php
//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 that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//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['comment']) == '') {
$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 = 'myemail#gmail.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
The JQuery validation does work.
<script type="text/javascript">
$(document).ready(function(){
$("form").validate();
});
</script>
The contact form:
<div class="grid_8">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<label for="name">Your Name:</label>
<div>
<input type="text" name="contactname" class="required" />
</div>
</div>
<div>
<label for="email">Your Email:</label>
<div>
<input type="text" name="email" class="required email" />
</div>
</div>
<div>
<label for="subject">Subject:</label>
<div>
<input type="text" name="subject" class="required" />
</div>
</div>
<div>
<label for="comments">Comments:</label>
<div>
<textarea name="comment" name="comment" class="required"></textarea>
</div>
</div>
<div>
<input id="button" type="submit" value="SEND" />
</div>
</form>
</div>
When you submit the form, the email does not get sent, nor any verification from php. What am I doing wrong?

One quick obvious issue, although I have many less critical ones. Simply put you'r checking for $_POST['submit'] put there is name="submit" in your form.
So change:
<input id="button" type="submit" value="SEND" />
To:
<input id="button" type="submit" name="submit" value="SEND" />
Or change:
if(isset($_POST['submit']))
To:
if(count($_POST))
// OR
if(!empty($_POST))
Either one will fix your problem

I don't see why isset($_POST['submit']) should return true when you have no field with name="submit" in your form.
What I prefer to do is using an array for all the required data:
<!-- ... -->
<input type="text" name="mail[contactname]" class="required" />
<!-- ... -->
<input type="text" name="mail[email]" class="required email" />
<!-- ... -->
<input type="text" name="mail[subject]" class="required" />
<!-- ... -->
<textarea name="comment" name="mail[comment]" class="required"></textarea>
<!-- ... -->
And then asking for that array
if (isset($_POST['mail']))
// ...

Related

How can I add file upload to my existing contact form [duplicate]

This question already has answers here:
Send attachments with PHP Mail()?
(16 answers)
Closed 7 years ago.
I have an existing html contact form, but I would like to add the feature to upload a photo to be sent as an attachment. How can I add that to my php script. I have already added the file upload field in the html form name=datafile and I have also set the attribute enctype="multipart/form-data"
<?php
$yourEmailAddress = '123#123.com'; //Put your own email address here.
//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 that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", 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 = $yourEmailAddress;
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $comments";
$headers = 'From: Interion Template <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
echo'<div id="success" class="sent success"><p><strong>Email Successfully Sent!</strong><br>Thanks for contacting Us. Your email was successfully sent and we \'ll be in touch with you soon.</p></div>';
} else { //If errors are found
echo '<p class="error">Please check if you\'ve filled all the fields with valid information and try again. Thank you.</p>';
}
?>
Here is my HTML part
<form method="post" action="sendMail.php" enctype="multipart/form-data" id="contactform">
<div class="response"> </div>
<p ><label for="contactname">What is your full legal Name?<span>*</span></label>
<input id="contactname" type="text" value="" name="contactname" placeholder="Full Name" class="textflied">
<i class="icon fa fa-user"></i></p>
<p><label for="email" >What is the best email address for us to contact you on?<span>*</span></label>
<input id="email" type="text" value="" name="email" placeholder="Email Address" class="textflied">
<i class="icon fa fa-envelope"></i></p>
<p><label for="subject" >Now tell us the best phone number to reach you at:<span>*</span></label>
<input id="subject" type="text" value="" name="subject" placeholder="Your phone number including area code" class="textflied">
<i class="icon fa fa-phone "></i></p>
<p><label for="message" >In a short paragraph how would you discribe your self?<span>*</span></label>
<textarea id="message" type="text" name="message" value="" placeholder="Your short paragraph" rows="8" class="texttextarea"></textarea>
<i class="icon fa fa-comments"></i></p>
<label for="datafile" >Finally lets match the name with a face, upload your most recent photo<span>*</span></label>
<input type="file" name="datafile" size="40">
<br>
<br>
<p>
<button type="submit" name="submit" id="submitButton" title="Click here to submit your message!" class="btn btn-disabled">SEND message</button>
</p>
</form>
please add this code and check it may help you
if(trim($_FILES['datafile']['name'])==''){
$hasError = true;
}
else {
$target='upload/';/*give the directory name wfere you want to save it*/
move_uploaded_file($_FILES['datafile']['tmp_name'],$target);
}
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?php
$yourEmailAddress = '123#123.com'; //Put your own email address here.
if(trim($_FILES['datafile']['name'])==''){
$hasError = true;
}
else {
$target='upload/';/*give the directory name wfere you want to save it*/
move_uploaded_file($_FILES['datafile']['tmp_name'],$target);
}
//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 that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", 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(!$hasError) {
$emailTo = $yourEmailAddress;
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $comments";
$headers = 'From: Interion Template <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
echo'<div id="success" class="sent success"><p><strong>Email Successfully Sent!</strong><br>Thanks for contacting Us. Your email was successfully sent and we \'ll be in touch with you soon.</p></div>';
} else { //If errors are found
echo '<p class="error">Please check if you\'ve filled all the fields with valid information and try again. Thank you.</p>';
}
?>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data" id="contactform">
<div class="response"> </div>
<p >
<label for="contactname">What is your full legal Name?<span>*</span></label>
<input id="contactname" type="text" value="" name="contactname" placeholder="Full Name" class="textflied">
<i class="icon fa fa-user"></i></p>
<p>
<label for="email" >What is the best email address for us to contact you on?<span>*</span></label>
<input id="email" type="text" value="" name="email" placeholder="Email Address" class="textflied">
<i class="icon fa fa-envelope"></i></p>
<p>
<label for="subject" >Now tell us the best phone number to reach you at:<span>*</span></label>
<input id="subject" type="text" value="" name="subject" placeholder="Your phone number including area code" class="textflied">
<i class="icon fa fa-phone "></i></p>
<p>
<label for="message" >In a short paragraph how would you discribe your self?<span>*</span></label>
<textarea id="message" type="text" name="message" value="" placeholder="Your short paragraph" rows="8" class="texttextarea"></textarea>
<i class="icon fa fa-comments"></i></p>
<label for="datafile" >Finally lets match the name with a face, upload your most recent photo<span>*</span></label>
<input type="file" name="datafile" size="40">
<br>
<br>
<p>
<button type="submit" name="submit" id="submitButton" title="Click here to submit your message!" class="btn btn-disabled">SEND message</button>
</p>
</form>
</body>
</html>

Email wont send with information entered and sends blank email on page refresh

I have done server side validation for my contact form and when the user fails to enter information into a required field an error occurs when the send button is pressed and no email is sent. I can't get the email to send if all the relevant information is input into the fields. The form is validated and sent on the same page. I have added the right script to send the email but on page refresh it sends a blank email and shows no errors.
<?php
// define variables and set to empty values
$firstnameErr = $secondnameErr = $emailaddressErr = $commentErr = $captchaErr = "";
$firstname = $email = $secondname = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
$firstnameErr = "First name is required";
} else {
$firstname = test_input($_POST["firstname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
$firstnameErr = "Invalid first name";
}
}
if (empty($_POST["secondname"])) {
$secondnameErr = "Second name is required";
} else {
$secondname = test_input($_POST["secondname"]);
// check if e-mail address syntax is valid
if (!preg_match("/^[a-zA-Z ]*$/",$secondname)) {
$secondnameErr = "Invalid second name";
}
}
if (empty($_POST["emailaddress"])) {
$emailaddressErr = "Email address is required";
} else {
$emailaddress = test_input($_POST["emailaddress"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$emailaddress)) {
$emailaddressErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$commentErr = "Enter a message";
} else {
$comment = test_input($_POST["comment"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$comment)) {
$commentErr = "Only letters and white space allowed";
}
}
if (empty($_POST["captcha"])) {
$captchaErr = "Enter the answer to the sum";
} else {
$captcha = test_input($_POST["captcha"]);
// check if name only contains letters and whitespace
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form name="Contact" form id="Contact" onsubmit=" return validate()" METHOD="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="Row">
<div class="Lable">First Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="firstname" class="detail" name="firstname" placeholder="First Name" />
<span class="error"><?php echo $firstnameErr;?></span> </div>
<!--End input-->
</div> <!--End row--><br />
<div class="Row">
<div class="Lable">Second Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="secondname" class="detail" name="secondname" placeholder="Second Name" />
<span class="error"><?php echo $secondnameErr;?></span> </div>
<!--End input-->
</div> <!--End row-->
<br />
<div class="Row">
<div class="Lable">Email Address:</div> <!--End of Lable-->
<div class="input">
<input type="email" id="emailaddress" class="detail" name="emailaddress" placeholder="Email Address" />
<span class="error"><?php echo $emailaddressErr;?></span>
</div> <!--End input-->
</div> <!--End row-->
<br />
<div class="Row">
<div class="Lable">Your Message:</div> <!--End of Lable-->
<div class="input">
<textarea id="comment" name="comment" class="mess" placeholder="Your Message" minlength="10" ></textarea>
<span class="error"><?php echo $commentErr;?></span>
</div> <!--End input-->
</div> <!--End row-->
<br />
<input id="number1" name="number1" readonly="readonly" class="Add" value="<?php echo rand(1,4) ?>" /> +
<input id="number2" name="number2" readonly="readonly" class="Add" value="<?php echo rand(5,9) ?>" /> =
<input type="text" name="captcha" id="captcha" class="captcha" maxlength="2" />
<div class="Lable">Please give the correct answer to the sum</div>
<br />
<span class="captchaerror"><?php echo $captchaErr;?></span>
<br />
<br />
<div class="submit">
<input type="submit" id="send" Name="send" value="Send" />
</div><!--End of submit-->
<div class="Clear">
<input type="reset" id="clear" Name="Clear" value="Clear" />
</div>
</form>
<?php
if (!empty($_POST)) {
}
else {
$firstname = $_POST["firstname"];
$secondname = $_POST["secondname"];
$email = $_POST["emailaddress"];
$comments = $_POST["comment"];
$message = "New Email for a customer" .
"\r\nName of the contact" .
"\r\n-". $firstname .
"\r\nName of the contact" .
"\r\n-". $secondname .
"\r\nEmail address of the contact" .
"\r\n-".$email .
"\r\nThe comment that the contact has made" .
"\r\n-".$comments .
$headers = "From: " . $email;
mail("kieran#localhost",$subject,$message,$headers);
$subjectReply = 'Thank you for your contact..';
$messageReply = 'You will soon receive an answer';
$headers = 'From: admin#shreddednutrition';
mail($email, $subjectReply, $messageReply, $headers);
}
?>
thanks in advance
Why:
if (!empty($_POST)) {
}
else {
so basically the code does nothing if there is some $_POST data..
Change to:
if (empty($_POST)) {
}
else {

PHP contact form will not submit

I have a simple php contact form i got from a web tutorial. It worked yesterday, but will not work today. I'd love some help, as I don;t know much php.
php:
<?php
//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 that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!filter_var( trim($_POST['email'], FILTER_VALIDATE_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 = 'person#domain.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
HTML:
<form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="alert alert-danger">Please check if you've filled all the fields with valid information and try again. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<div class="alert alert-success">
<p><strong>Message Successfully Sent!</strong></p>
<p>Thank you for using our contact form, <strong><?php echo $name;?></strong>! Your email was successfully sent and we’ll be in touch with you soon.</p>
</div>
<?php } ?>
<div class="form-group">
<label for="name">Your Name<span class="help-required">*</span></label>
<input type="text" name="contactname" id="contactname" value="" class="form-control required" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="email">Your Email<span class="help-required">*</span></label>
<input type="text" name="email" id="email" value="" class="form-control required email" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>
<div class="form-group">
<label for="message">Message<span class="help-required">*</span></label>
<textarea rows="8" name="message" id="message" class="form-control required" role="textbox" aria-required="true"></textarea>
</div>
<div class="actions">
<input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-grey" title="Click here to submit your message!" />
<input type="reset" value="Clear Form" class="btn btn-grey pull-right" title="Remove all the data from the form." />
</div>
</form>
It gets hung up on the validation. Not sure why.
$_POST["subject] is not defined in your form. Your SUBJECT field is called EMAIL:
Change:
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>
With:
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="subject" id="subject" class="form-control required" role="input" aria-required="true">
</div>

echo an error message for each input in form

i have a form with input for name and e-mail. I would like to have a dedicated error-message for each input - fx
if no name is written it will say "please write your name!"
and if the e-mail is missing or not valid it echoes "wrong mail - try again!"
At the moment I have only 1 error-message that will echo for both situations.
How can i assign an dedicated error-message for each of the inputs?
Heres the code:
<?php
//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']);
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = '##gmail.com'; //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;
}
}
?>
<article class="kontakt">
<?php if($_POST['contactname'] != '') { //echo when a name was entered ?>
<!--<p> Hello </p>-->
<?php $name = strip_tags(trim($_POST['contactname']));
echo $name;
} ?>
</article>
<article class="kontakt">
<?php if(isset($hasError)) { // THIS PART IS ECHOED IN BOTH SITUATIONS - should only apply for error in e-mail?>
<p class="error"> Your mail is <span style="color: orange"> not correct</span> - try again! </p>
<?php } ?>
<?php if($_POST['email'] != '') { // echo when a valid mail was entered ?>
<p> Hello </p>
<?php $name = strip_tags(trim($_POST['email']));
echo $email;
} ?>
</article>
<article class="kontakt">
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p> Your message is sent !</p>
<?php } ?>
</article>
}
?>
try something like:
if(trim($_POST['contactname']) == '') {
$hasError['contactname'] = 'Please enter a contact name!';
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError['email'] = 'Please enter email!';
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError['validemail'] = 'Please enter valid email';
} else {
$email = trim($_POST['email']);
}
and this to display errors:
if(isset($hasError)){
echo '<ul>';
foreach($hasError as $error){
echo '<li>'.$error.'</li>';
}
echo '</ul>';
}
and this to keep old values upon error
<div class="right_wrap">
<div class="header"> <h5><span style="color: white"> mail </span></h5> </div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<article class="kontakt">
<label for="name"> Dit navn </label>
<input type="text" name="contactname" id="contactname" value="<?php echo $_POST['contactname']; ?>" class="" />
</article>
<article class="kontakt">
<label for="email"> Din mail </label>
<input type="text" name="email" id="email" value="<?php echo $_POST['email']; ?>" class="required email" />
</article>
<article class="kontakt" style="height: auto">
<label for="message"> Din besked </label>
<textarea rows="5" cols="50" name="message" class="required"><?php echo $_POST['message']; ?></textarea>
</article>
<article class="kontakt">
<input type="submit" value="Send besked" name="submit" class="button"/>
</article>
</form>
</div> <!--end of right_wrap -->
You treat each input with individual code that means you don't have much data-structure here.
Normally some kind of model for inputs as well as associated errors and values is helpful to get things done with forms.
A lightweight entry that normally works pretty well with individual PHP scripts is HTML_QuickForm2.
It does take care of individual error messages as well. You naturally can write that your own and luckily it is free software so you are allowed to study the code and learn from it.
Heres the actual form - some danish words in it. Whatever errors / messages will be output in a left wrap div - so cant be placed inside the actual form.
<div class="right_wrap">
<div class="header"> <h5><span style="color: white"> mail </span></h5> </div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<article class="kontakt">
<label for="name"> Dit navn </label>
<input type="text" name="contactname" id="contactname" value="" class="" />
</article>
<article class="kontakt">
<label for="email"> Din mail </label>
<input type="text" name="email" id="email" value="" class="required email" />
</article>
<article class="kontakt" style="height: auto">
<label for="message"> Din besked </label>
<textarea rows="5" cols="50" name="message" class="required"></textarea>
</article>
<article class="kontakt">
<input type="submit" value="Send besked" name="submit" class="button"/>
</article>
</form>
</div> <!--end of right_wrap -->

How to make a send yourself a copy check box

I need a check box that can send yourself a copy. For example, they've filled out the contact form and want a copy sent to their self, they check the box and it will email it to me and still email it to them. Here's my PHP:
//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 that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['weburl']) == '') {
$site = trim($_POST['weburl']);
}
//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 = 'myemail#domain.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nSite: \n\nComments:\n $comments";
$headers = 'From: BTSyncrets Contact <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
Here's my contact form code:
<div id="contact" class="offset4 login">
<form style="margin-top: 5% !important;" method="post" action="index.php" id="contactform">
<fieldset class="well">
<br>
<div class="clearfix">
<label for="name">
Your Name<span class="help-required">*</span>
</label>
<div class="input">
<input type="text" id="boxblack" name="contactname" id="contactname" value="" class="span6 required" role="input" aria-required="true" />
</div>
</div>
<div class="clearfix">
<label for="email">
Your Email<span class="help-required">*</span>
</label>
<div class="input">
<input type="text" id="boxblack" name="email" id="email" value="" class="span6 required email" role="input" aria-required="true" />
</div>
</div>
<div class="clearfix">
<label for="weburl">
Your Website
</label>
<div class="input">
<input type="text" id="boxblack" name="weburl" id="weburl" value="" class="span6 required url" role="input" aria-required="true" />
</div>
</div>
<div class="clearfix">
<label for="subject">
Subject<span class="help-required">*</span>
</label>
<div class="input">
<select name="subject" id="boxblack" id="subject" class="span6 required" role="select" aria-required="true">
<option></option>
<option>One</option>
<option>Two</option>
</select>
</div>
</div>
<div class="clearfix">
<label for="message">Message<span class="help-required">*</span></label>
<div class="input">
<textarea rows="8" id="boxblack" style="resize: none;" name="message" id="message" class="span6 required" role="textbox" aria-required="true"></textarea>
</div>
</div>
<label class="checkbox">
<input type="checkbox" name="copy" value="1" /> Send Yourself a copy
</label>
<div class="actions">
<input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-inverse" title="Click here to submit your message!" />
<input type="reset" value="Clear Form" class="btn btn-danger" title="Remove all the data from the form." />
</div>
</fieldset>
</form>
</div><!-- form -->
See the Add this comment below.
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail#domain.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nSite: \n\nComments:\n $comments";
$headers = 'From: BTSyncrets Contact <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
// Add this
if (isset($_POST['copy'])) {
$headers .= "\nBcc: myemailaddress#example.com";
}
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}

Categories