How to make a send yourself a copy check box - php

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;
}

Related

Prevent Resubmissions for PHP Contact form

This is the code for the form.
<?php
if ($_POST) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$checkbox = '';
if (isset($_POST['checkbox'])) {
$checkbox = 'Yes';} else{
$checkbox = 'No' ;}
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'imvael#gmail.com, vnikolic1#cps.edu';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Company: $company\n Phone: $phone\n Opt In?: $checkbox\n Message:\n $message";
$headers = 'From: webmaster#bradfordsystems.com' . "\r\n" .
'Reply-To: ' .$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// 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 name has been entered
if (!$_POST['phone']) {
$errName = 'Please enter your phone number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// then after the posting of the form data
// validation
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && isset($_POST['url']) && $_POST['url'] == '') {
if (mail ($to, $subject, $body, $headers)) {
header("Location: thankyou.php"); /* Redirect browser */
exit();
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
This is the form itself
<div class="wrapper">
<div class="row">
<div class="col _66">
<?php echo $result; ?>
<form role="form" name="contactForm" id="contactForm" method="post" action="contact.php#contactForm">
<p>
We welcome your feedback and inquiries. Please use the form below to get in touch.
</p>
<div class="row">
<div class="col">
<input type="text" id="name" name="name" placeholder="Name" value="<?php echo htmlspecialchars($_POST['name']); ?>" required>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="col">
<input type="email" id="email" name="email" placeholder="Company Email" value="<?php echo htmlspecialchars($_POST['email']); ?>" required>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="text" id="company" name="company" placeholder="Company" value="<?php echo htmlspecialchars($_POST['company']); ?>" required>
</div>
<div class="col">
<input type="tel" id="phone" name="phone" <?php echo htmlspecialchars($_POST['phone']); ?> placeholder="Phone" required>
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="number" id="zipcode" name="zipcode" placeholder="Zip Code" value="<?php echo htmlspecialchars($_POST['zipcode']); ?>">
</div>
<div class="col">
<input id="checkBox" type="checkbox"> <span id="optInText">YES, I want a Free Workspace Evaluation!</span>
</div>
</div>
<div class="row">
<div class="col">
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
</div>
</div>
<div class="row">
<div class="col submit-col">
<p>Questions or Comments?</p>
<textarea id="message" name="message" placeholder="Enter your questions or comments here" style="height:200px"> <?php echo htmlspecialchars($_POST['message']); ?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
<button class="btn btn-dark hvr-underline-from-left" name="submit" type="submit" value="Send">Submit Request</button>
</div>
</div>
</form>
</div>
</div>
</section>
I am writing a contact form script from scratch The code is working correctly, but I am unable to prevent resubmissions if they user refreshed the page or press the submit buton more then once.
Also is it a bad practice to use self submitting contact forms?

My PHP contact from send me emails but leaves part of it blank

I am using a theme and this is the php that came with it. I am testing out the emails and one thing I am noticing is that a few sections are contniuosly popping up blank no matter what I do. This is my first time using php and Im not sure if I should delete the themes php and create my own. Specifically the Number,Address,City,State,Date,Zip Code are the texts that show up blank. I receive the emails but those sections are continuously left blank. Is my code not correct?
<php>
<?php
if ($_POST['fname']) {
// Your Email
$recipient = "thttkidd#yahoo.com"; // PLEASE SET YOUR EMAIL ADDRESS
// Check $recipient
if($recipient === '') {
returnAndExitAjaxResponse(
constructAjaxResponseArray(
FALSE,
'RECIPIENT_IS_NOT_SET',
array('error_message'=> 'RECIPIENT email address is not set. Please configure the script.')
)
);
}
// Check for empty required field
if(!isset($_POST["email"]) || !isset($_POST["fname"]) || !isset($_POST["message"])) {
returnAndExitAjaxResponse(
constructAjaxResponseArray(
FALSE,
'MISSING_REQUIRED_FIELDS',
array('error_message'=> 'MISSING_REQUIRED_FIELDS should not be occurred.')
)
);
}
// Sanitize input
$fname = filter_var($_POST["fname"], FILTER_SANITIZE_STRING);
$lname = filter_var($_POST["lname"], FILTER_SANITIZE_EMAIL);
$website = $_POST["website"];
if (!preg_match("~^(?:f|ht)tps?://~i", $website)) $website = "http://" . $website;
$website = filter_var($website, FILTER_VALIDATE_URL);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
$number = filter_var($_POST["number"], FILTER_SANITIZE_STRING);
$adress = filter_var($_POST["Adress"], FILTER_SANITIZE_STRING);
$City = filter_var($_POST["City"], FILTER_SANITIZE_STRING);
$State = filter_var($_POST["State"], FILTER_SANITIZE_STRING);
$Date = filter_var($_POST["date"], FILTER_SANITIZE_STRING);
$Zcode = filter_var($_POST["ZCode"], FILTER_SANITIZE_STRING);
var_dump($_POST);
// If non required fields are empty
if ( empty($lname) ){
$lname = "No last name entered.";
}
// Headers
$headers = 'From: '.$fname.' <'.$email.'>' . "\r\n";
$headers .= 'Reply-To: '.$email.'' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
// Subject
$subject = "New email from book now form";
// Build Message
$email_content = "First Name: $fname\n";
$email_content .= "Last Name: $lname\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Number: $number\n";
$email_content .= "Address: $adress\n\n";
$email_content .= "City: $City\n\n";
$email_content .= "State $State\n\n";
$email_content .= "Date $Date\n\n";
$email_content .= "Zcode $Zcode\n\n";
$email_content .= "Message:\n$message\n\n\n";
$email_content .= "CLIENT IP:\n".get_client_ip()."\n";
$email_content .= "HOST IP:\n".$_SERVER['SERVER_ADDR']."\n";
// Check if sent
try {
$sendmailResult = mail($recipient, $subject, $email_content, $headers);
if( $sendmailResult === TRUE ) {
returnAndExitAjaxResponse(
constructAjaxResponseArray(
TRUE
)
);
} else {
returnAndExitAjaxResponse(
constructAjaxResponseArray(
FALSE,
'ERROR_AT_PHPMAIL',
array('error_information'=> error_get_last() )
)
);
}
} catch (Exception $_e) {
returnAndExitAjaxResponse(
constructAjaxResponseArray(
TRUE,
'ERROR_AT_PHPMAIL',
array('error_message'=> $_e->getMessage())
)
);
}
}
/*
Construct ajax response array
Input: Result (bool), Message (optional), Data to be sent back in array
*/
function constructAjaxResponseArray ($_response, $_message = '', $_json = null) {
$_responseArray = array();
$_response = ( $_response === TRUE ) ? TRUE : FALSE;
$_responseArray['response'] = $_response;
if(isset($_message)) $_responseArray['message'] = $_message;
if(isset($_json)) $_responseArray['json'] = $_json;
return $_responseArray;
}
/*
Returns in the Gframe ajax format.
Input: data array processed by constructAjaxResponseArray ()
Outputs as a html stream then exits.
*/
function returnAndExitAjaxResponse ($_ajaxResponse) {
if(!$_ajaxResponse){
$_ajaxResponse = array('response'=>false,'message'=>'Unknown error occurred.');
}
header("Content-Type: application/json; charset=utf-8");
echo json_encode($_ajaxResponse);
die();
}
// Function to get the client IP address
function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if(isset($_SERVER['HTTP_X_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} else if(isset($_SERVER['HTTP_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} else if(isset($_SERVER['REMOTE_ADDR'])) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
} else {
$ipaddress = 'UNKNOWN';
}
return $ipaddress;
}
?>
< HTML >
<!--Contact Form -->
<section class="section-block replicable-content contact-2 no-padding-top">
<div class="row">
<div class="column width-8 offset-2 center">
<h2 class="mb-30"><strong>Book Your Appointment Now</strong></h2>
<div class="contact-form-container">
<form class="contact-form" action="" method="post" novalidate>
<div class="row">
<div class="column width-6">
<input type="text" name="fname" class="form-fname form-element large" placeholder="First Name*" tabindex="1" required>
</div>
<div class="column width-6">
<input type="text" name="lname" class="form-lname form-element large" placeholder="Last Name*" tabindex="2" required>
</div>
<div class="column width-6">
<input type="email" name="email" class="form-email form-element large" placeholder="Email address*" tabindex="3" required>
</div>
<div class="column width-6">
<input type="number" name="number" class="form-number form-element large" placeholder="Phone*" tabindex="4" required>
</div>
<div class="column width-6">
<input type="text" name="Adress" class="form-address form-element large" placeholder="Street Address*" tabindex="5" required>
</div>
<div class="column width-6">
<input type="text" name="City" class="form-city form-element large" placeholder="City*" tabindex="6" required>
</div>
<div class="column width-6">
<input type="text" name="State" class="form-state form-element large" placeholder="State*" tabindex="7" required>
</div>
<div class="column width-6">
<input type="text" name="ZCode" class="form-zcode form-element large" placeholder="Zip Code*" tabindex="8" required>
</div>
<div class="column width-6">
<input type="date" name="date" class="form-date form-element large" placeholder="Date*" tabindex="9" >
</div>
<div class="column width-6">
<div class="form-select form-element large">
<select name="options" class="form-aux" data-label="Options" tabindex="10" required>
<option>Time Window</option>
<option value="">10AM-12PM</option>
<option value="">12PM - 2PM</option>
<option value="">2PM - 4PM</option>
<option value="">4PM - 6PM</option>
</select>
</div>
</div>
<div class="column width-12">
<input type="junk" name="junk" class="form-junk form-element large" placeholder="Where Is Your Junk Located Ex. (Attic, Backyard,Shed, Front Yard, Inside Home Etc.)*" tabindex="9" required>
</div>
<div class="column width-12">
<div class="form-select form-element large">
<select name="options" class="form-aux" data-label="Options" tabindex="5">
<option selected="selected" value="" >How'd You Find Us</option>
<option value="">From A Friend</option>
<option value="">Google</option>
<option value="">Bing</option>
<option value="">Kudzo</option>
<option value="">Yelp</option>
<option value="">Yahoo</option>
<option value="">Angie's List</option>
<option value="">Other</option>
</select>
</div>
</div>
<div class="column width-6">
<input type="text" name="honeypot" class="form-honeypot form-element large">
</div>
</div>
<div class="row">
<div class="column width-12">
<div class="field-wrapper">
<textarea name="message" class="form-message form-element large" placeholder="Briefly Describe what you need removed*" tabindex="7" required></textarea>
</div>
</div>
<div class="column width-12">
<input type="submit" value="Book Now" class="form-submit button medium bkg-theme bkg-hover-theme color-white color-hover-white">
</div>
</div>
</form>
<div class="form-response center"></div>
</div>
</div>
</div>
</section>
<!--Contact Form End -->
It seems the variables are not getting any value from $_POST
So, right below the line // Sanitize input add these lines
$number = filter_var($_POST["number"], FILTER_SANITIZE_STRING);
$adress = filter_var($_POST["Adress"], FILTER_SANITIZE_STRING);
$City = filter_var($_POST["City"], FILTER_SANITIZE_STRING);
$State = filter_var($_POST["State"], FILTER_SANITIZE_STRING);
$Date = filter_var($_POST["date"], FILTER_SANITIZE_STRING);
$Zcode = filter_var($_POST["ZCode"], FILTER_SANITIZE_STRING);
Have you tried contacting support for your theme? They would likely have the info you (and we) would need in terms of how it's supposed to work, what environment is required (i.e. PHP5.6 vs PHP7.1, which modules enabled/disabled), and it would be good to know what environment you're using as well.

How do I stop the Phantom Emails from my PHP code?

I'm currently using an html5 website with a contact form and a php action to receive contact emails. I've tried a few different codes in my php but non have stopped the blank emails from coming. I do have google analytics enables on my code but have blocked the crawler through robot.txt. Here is my code.
PHP CODE
<?php
foreach ($_GET as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n";
}
}
$name = trim(htmlentities($_GET['name'],ENT_QUOTES,'utf-8'));
$email = trim(htmlentities($_GET['email'],ENT_QUOTES,'utf-8'));
$phone = trim(htmlentities($_GET['phone'],ENT_QUOTES,'utf-8'));
$messages = trim(htmlentities($_REQUEST['messages'],ENT_QUOTES,'utf-8'));
if (strlen($name) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
if (strlen($email) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
$to = "junior.8791#gmail.com";
$message = "Name: ".$name;
$message.="\n\nEmail: ".$email;
$message.="\n\nPhone: ".$phone;
$message .= "\n\nMessage: ".$messages;
$headers = "From: $email";
$headers .="\nReply-To: $email";
$success = mail($to, $subject, $message, $headers);
if ($success) {
echo "<script>window.location = 'http://www.mason372.org/thankyou.html'</script>";
} else {
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
?>
CONTACT FORM
<form action="email.php" method="post" id="form">
<div class="5grid">
<div class="row">
<div class="6u">
<input type="text" class="name" name="name" id="name" placeholder="Name" value="" aria-describedby="name-format" required aria-required=”true” pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+" title="e.g.'John Doe'" required="" />
</div>
<div class="6u">
<input type="email" class="email" name="email" id="email" placeholder="Email" required="" />
</div>
</div>
<div class="row">
<div class="12u">
<input type="tel" class="tel" name="phone" id="phone" name="phone" type="text" placeholder="Phone Number" pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" required />
</div>
</div>
<div class="row">
<div class="12u">
<textarea name="messages" id="messages" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" class="button" value="Send Message">
<input type="reset" class="button button-alt" value="Clear Form">

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>

Contact Form: Why won't this form validate?

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']))
// ...

Categories