PHP form checkbox - php

I'm havig a really bad time with a checkbox contact form. Now If I click sent in my webpage I have this result in my email:
Email: whatever#whatever.com
Nombre: whatever
Le interessa: (THIS IS
CHECKBOX AND IT'S EMPTY VALUES. I WOULD LIKE TO TAKE THE CHOICES THAT THE USER MAKE)
Otros intereses: whatever
Thank you, I'll apreciate some help
HTML->
<form name="form-contact-us" id="form-contact-us">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="txtName">Name: </label>
<input type="text" placeholder="Name"
class="form-control required" id="txtName" name="txtName" />
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label for="txtEmail">Email:</label>
<input type="text" placeholder="E-mail ID"
class="form-control required email" id="txtEmail" name="txtEmail" />
</div>
</div>
</div>
<label for="txtMessage">Información:</label><br>
***<input type="checkbox" id="web" name="check[]" value="Creación de web">
<label for="web">Creación de web</label><br>
<input type="checkbox" id="red" name="check[]" value="Gestión de reputación Online / Redes Sociales">
<label for="red">Gestión de reputación Online / Redes Sociales</label><br>
<input type="checkbox" id="seo" name="check[]" value="Gestión Estratégica (SEO, e-mail marketing)">
<label for="seo">Gestión Estratégica (SEO, e-mail marketing)</label><br><br>***
<div class="form-group">
<label for="txtMessage">Otros:</label>
<textarea placeholder="Message" class="form-control required"
id="txtMessage" name="txtMessage" rows="10"></textarea>
</div>
<div class="form-group">
<label for="txtCaptcha"></label>
<input
type="text" class="form-control required captcha"
placeholder="Are you human?" id="txtCaptcha" name="txtCaptcha" />
</div>
<div class="form-group">
<input type="button" id="btn-Send" value="Send" class="btn btn-theme-inverse" />
</div>
<div class="col-lg-12" id="contact-form-message"></div>
</form>
PHP->
<?php
$formType = $_POST["formType"];
$MailTo = 'victor.fernandez.gayan#gmail.com';
$txtName = $_POST["txtName"];
$txtEmail = $_POST["txtEmail"];
$txtRating = "";
$txtWebsite = "";
$txtMessage = $_POST["txtMessage"];
$putavariable = $_POST["check"];
if(!(empty($_POST['check']))){
foreach($_POST['check'] as $value){
$check_msg .= "Persona interesada en: $value\n";
}
}
if ($formType == "comments") {
$txtWebsite = $_POST["txtWebsite"];
}
if ($formType == "reviews") {
$txtRating = $_POST["txtRating"];
}
$success = TRUE;
try{
if(trim($txtEmail)==NULL){
throw new Exception("Please enter your e-mail id.");
}
else
{
if (!preg_match("/\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*/", trim($txtEmail)))
{
throw new Exception("Please enter the valid e-mail.");
}
}
if(trim($txtName)==NULL){
throw new Exception("Please enter your name.");
}
if(trim($txtMessage)==NULL){
throw new Exception("Please enter your message.");
}
if($formType == "contact") {
$MailSubject = "New Contact Mail";
$MailBody = "<strong>Email: </strong> ".$txtEmail."<br />";
$MailBody .= "<strong>Nombre: </strong> ".$txtName."<br />";
$MailBody .= "<strong>Le interessa: </strong> ".$putavariable."<br />";
$MailBody .= "<strong>Otros intereses: </strong> ".$txtMessage."<br />";
}
if($formType == "comments") {
$MailSubject = "New Comment Mail";
$MailBody = "<strong>Email: </strong> ".$txtEmail."<br />";
$MailBody .= "<strong>Name: </strong> ".$txtName."<br />";
$MailBody .= "<strong>Website: </strong> ".$txtWebsite."<br />";
$MailBody .= "<strong>Comment: </strong> ".$txtMessage."<br />";
}
if($formType == "reviews") {
$MailSubject = "New Review Mail";
$MailBody = "<strong>Email: </strong> ".$txtEmail."<br />";
$MailBody .= "<strong>Name: </strong> ".$txtName."<br />";
$MailBody .= "<strong>Rating: </strong> ".$txtRating."<br />";
$MailBody .= "<strong>Review: </strong> ".$txtMessage."<br />";
}
$MailFrom = $txtEmail;
$MailHeaders = 'MIME-Version: 1.0' . "\r\n";
$MailHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$MailHeaders .= "From: <".$txtEmail.">"."\r\n";
$result = mail($MailTo, $MailSubject, $MailBody, $MailHeaders);
if(!$result){
throw new Exception("Email no enviado:(");
}
$success = TRUE;
}
catch (Exception $e){
header("Status: 400 Bad Request", TRUE, 400);
echo($e->getMessage());
$success = FALSE;
}
if ($success)
{
header("Status: 200 OK", TRUE, 200);
// add your custom success message here
echo("EMAIL ENVIADO :)");
}
else
{
header("Status: 400 Bad Request", TRUE, 400);
}
?>

I revised my form:
<label for="txtMessage">Información:</label><br>
<input type="checkbox" id="web" name="check[]" value="Creación de web">
<label for="web">Creación de web</label><br>
<input type="checkbox" id="red" name="check[]" value="Gestión de reputación Online / Redes Sociales">
<label for="red">Gestión de reputación Online / Redes Sociales</label><br>
<input type="checkbox" id="seo" name="check[]" value="Gestión Estratégica (SEO, e-mail marketing)">
<label for="seo">Gestión Estratégica (SEO, e-mail marketing)</label><br><br>
<div class="form-group">
<label for="txtMessage">Otros:</label>
<textarea placeholder="Message" class="form-control required" id="txtMessage" name="txtMessage" rows="10"></textarea>
and PHP:
$formType = $_POST["formType"];
$MailTo = 'victor.fernandez.gayan#gmail.com';
$txtName = $_POST["txtName"];
$txtEmail = $_POST["txtEmail"];
$txtRating = "";
$txtWebsite = "";
$txtMessage = $_POST["txtMessage"];
$putavariable = $_POST["check"];
var_dump($_POST);
if(!(empty($_POST['check']))){
foreach($_POST['check'] as $value){
$check_msg .= "Persona interesada en: $value\n";
}
}
And result:
array(5) { ["formType"]=> string(7) "contact" ["txtName"]=> string(10) "Imchecking" ["txtEmail"]=> string(25) "Imchecking#Imchecking.com" ["txtMessage"]=> string(16) "hello Imchecking" ["txtMailTo"]=> string(0) "" } EMAIL SENDED :)
I thing I'm close but I dont know how to procee it. I checked the checkboxes on before sent the email but nothing happens.

Related

post 405 method not allowed php forn

I have a simple form for a website in HTML and PHP query.
HTML form
<form name="contactForm" id="contactForm" method="post" action="" >
<fieldset>
<div class="group">
<input name="contactName" type="text" id="contactName" placeholder="Name" value="" minLength="2" required />
</div>
<div>
<input name="contactEmail" type="email" id="contactEmail" placeholder="Email" value="" required />
</div>
<div>
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" />
</div>
<div>
<textarea name="contactMessage" id="contactMessage" placeholder="message" rows="10" cols="50" required ></textarea>
</div>
<div>
<button class="submitform contactdetails1">Submit</button>
<div id="submit-loader">
<div class="text-loader contactdetails2">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
After trying to submit the form, I have the error messages that POST 405 NOT ALLOWED. It is unclear from the documentation how to set up a server and fix POST request.
PHP query
<?php
// Replace this with your own email address
$siteOwnersEmail = 'jeremie.xxxxx#xxxxx.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
...................................................................

My Email code is not working when i am replacing

When i am replacing the email with my email id, it shows "?ok" and if i am leaving it default it shows "Your emails was Sent Successfully"
i guess there is something wrong with php code, when i edit it shows "?ok", and when i reset it back it agains shows "?ok" until i don't upload the original file which i edited (it has same code, just not opened or edited)
Here is my Php code:
<?php
// Replace this with your own email address
$siteOwnersEmail = 'user#website.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
HTML CODE:
<div class="contact-primary">
<h3 class="h6">Send Us A Message</h3>
<form name="contactForm" id="contactForm" method="post" action="" novalidate="novalidate">
<fieldset>
<div class="form-field">
<input name="contactName" type="text" id="contactName" placeholder="Your Name" value="" minlength="2" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactEmail" type="email" id="contactEmail" placeholder="Your Email" value="" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" class="full-width">
</div>
<div class="form-field">
<textarea name="contactMessage" id="contactMessage" placeholder="Write us a summary for your business and what we can do to help" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
</div>
<div class="form-field">
<button class="full-width btn--primary">Submit</button>
<div class="submit-loader">
<div class="text-loader">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
</fieldset>
</form>
<!-- contact-warning -->
<div class="message-warning">
Something went wrong. Please try again.
</div>
<!-- contact-success -->
<div class="message-success">
Your message was sent, thank you!<br>
</div>
</div> <!-- end contact-primary -->
Please suggest what are the changes i need to do to make work.
you can check here how error shows https://freesoft64.com/test.html

Redirect to separate succes page after form submit

I am using bootstrap and have a form I've been using for a while now. It shows the message "Verzonden!" on the same page if the form has been submitted succesfully. now this has to be altered to redirect to a new succes page after the form is submitted.
this is the html I am using
<form role="form" id="contactForm" class="contact-form" data-toggle="validator" class="shake">
<div class="form-group">
<div class="controls">
<input type="text" id="name" class="form-control" placeholder="Naam & Voornaam" required data-error="Gelieve uw naam in te vullen.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="email" class="email form-control" id="email" placeholder="Email" required data-error="Gelieve uw email adres in te vullen.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" id="phone" class="form-control" placeholder="Telefoonnummer" required data-error="Gelieve uw telefoonnummer in te vullen.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" id="msg_subject" class="form-control" placeholder="Onderwerp" required data-error="Gelieve een onderwerp in te vullen.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<textarea id="message" rows="7" placeholder="Bericht" class="form-control" required data-error="Gelieve uw bericht in te vullen."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<button type="submit" id="submit" class="btn btn-success"></i>Verzend!</button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
</form>
This is the php I am using:
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Gelieve uw naam in te vullen.";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Gelieve uw email adres in te vullen.";
} else {
$email = $_POST["email"];
}
// PHONE
if (empty($_POST["phone"])) {
$errorMSG .= "Gelieve uw telefoonnummer in te vullen.";
} else {
$phone = $_POST["phone"];
}
// MSG SUBJECT
if (empty($_POST["msg_subject"])) {
$errorMSG .= "Gelieve een onderwerp in te vullen.";
} else {
$msg_subject = $_POST["msg_subject"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Gelieve een bericht in te vullen";
} else {
$message = $_POST["message"];
}
//Add your email here
$EmailTo = "e-mail address";
$Subject = "Nieuw bericht van website";
// prepare email body text
$Body = "";
$Body .= "Naam: ";
$Body .= "\n";
$Body .= $name;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= "\n";
$Body .= $email;
$Body .= "\n";
$Body .= "\n";
$Body .= "Telefoonnummer: ";
$Body .= "\n";
$Body .= $phone;
$Body .= "\n";
$Body .= "\n";
$Body .= "Onderwerp: ";
$Body .= "\n";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "\n";
$Body .= "Bericht: ";
$Body .= "\n";
$Body .= $message;
$Body .= "\n";
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "Verzonden!";
}else{
if($errorMSG == ""){
echo "Er is een fout opgetreden, probeert u aub opnieuw.";
} else {
echo $errorMSG;
}
}
?>
You have to redirect the user to a new page using the php header function:
// redirect to success page
if ($success && $errorMSG == ""){
header('Location: http://www.example.com/success-page.php');
}else{
if($errorMSG == ""){
echo "Er is een fout opgetreden, probeert u aub opnieuw.";
} else {
echo $errorMSG;
}
}
IMPORTANT NOTE
Never print or echo anything before the header() redirect call or else it will not redirect the user.
Read the reference here:
http://php.net/manual/en/function.header.php
UPDATE
// redirect to success page
if ($success && $errorMSG == ""){
header('Location: http://www.example.com/success-page.php');
}else{
if($errorMSG == ""){
header('Location: http://www.example.com/success-page.php');
} else {
echo $errorMSG;
}
}
also added condition for if($errorMSG == "")
Also
It is a good practice to add the action attribute in your form like so:
<form action="path-to-your-processing-script.php">
If you don't provide that it will submit the form on the same page but still you should give it anyway.

My contact form is not sending any emails [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I was working on my own vCard/resume website.
But I am having some trouble with my .php file and contact form.
I tried to debug it but I had no success.
Here is my form in html
<!-- form -->
<form name="contactForm" id="contactForm" method="post" action="inc/sendEmail.php">
<fieldset>
<div class="form-field">
<input name="contactName" type="text" id="contactName" placeholder="Name" value="" minlength="2" required>
</div>
<div class="form-field">
<input name="contactEmail" type="email" id="contactEmail" placeholder="Email" value="" required>
</div>
<div class="form-field">
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="">
</div>
<div class="form-field">
<textarea name="contactMessage" id="contactMessage" placeholder="Message" rows="10" cols="50" required></textarea>
</div>
<div class="form-field">
<button class="submitform">Submit</button>
<div id="submit-loader">
<div class="text-loader">
Sending...
</div>
<div class="s-loader">
<div class="bounce1">
</div>
<div class="bounce2">
</div>
<div class="bounce3">
</div>
</div>
</div>
</div>
</fieldset>
</form>
<!-- Form End -->
And my php file
<?php
// Replace this with your own email address
$siteOwnersEmail = 'someone#example.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
Can someone tell me what I am doing wrong here, because it isn't sending any emails.
Please try this
<?php
$message ="";
if(isset($_POST)){
mail(to,subject,message,headers,parameters);
}
?>
You have to declare type of button type="submit" OR type="button"
in this case work with form so you have to add type="submit"
and check user click button or direct access of file you can check !empty($_POST) with if
// Change in your HTML
<button class="submitform">Submit</button>
// to
<button type="submit" class="submitform">Submit</button>
//Change in your PHP
if($_POST)
// to
if(!empty($_POST))

PHP - Mail script (Select element)

I have been working on a website recently from a website template (Puremedia). The template originally had a working contact form. It consisted of "First name", "last name", "Email" and "Subject". And then of course the message. I have now replaced the "Last name" text-box with a dropdown menu. I have done so by using the html select tags. Now my question is how I would change the php scripting so that instead of sending the message (Contact form) to One predefined email. It can send it to two different email adresses. How I would like it to be implemented would be using the dropdown menu. So for example the dropdown menu would be someling along these lines: "Choose Receiver" , "Mail 1" , "Mail 2".
And then also adding a checkbox below the message field that will send a copy to the persons own email address. - Here is my contact form code:
<form name="contactForm" id="contactForm" method="post" action="">
<fieldset>
<div class="row">
<div class="six columns mob-whole">
<label for="contactFname">First Name <span class="required">*</span></label>
<input name="contactFname" type="text" id="contactFname" placeholder="First Name" value="" />
</div>
<div class="six columns mob-whole">
<select name="mailacc">
<option value="1">Send til (Vælg venligst)</option>
<option value="2">Direktør</option>
<option value="3">Bogholder</option>
</select>
</div>
</div>
<div class="row">
<div class="six columns mob-whole">
<label for="contactEmail">Email <span class="required">*</span></label>
<input name="contactEmail" type="text" id="contactEmail" placeholder="Email" value="" />
</div>
<div class="six columns mob-whole">
<label for="contactSubject">Subject</label>
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" />
</div>
</div>
<div class="row">
<div class="twelve columns">
<label for="contactMessage">Message <span class="required">*</span></label>
<textarea name="contactMessage" id="contactMessage" placeholder="Your Message" rows="10" cols="50" ></textarea>
</div>
</div>
<div>
<button class="submit full-width">Send Message</button>
<div id="image-loader">
<img src="images/loader.gif" alt="" />
</div>
</div>
</fieldset>
</form> <!-- /contactForm -->
<!-- message box -->
<div id="message-warning"></div>
<div id="message-success">
<i class="fa fa-check"></i>Your message was sent, thank you!<br />
</div>
</div> <!-- /contact-form -->
The PHP part:
<?php
// Replace this with your own email address
$siteOwnersEmail = 'mail#domain.com';
if($_POST) {
$fname = trim(stripslashes($_POST['contactFname']));
$lname = trim(stripslashes($_POST['contactLname']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check First Name
if (strlen($fname) < 2) {
$error['fname'] = "Please enter your first name.";
}
// Check Last Name
if (strlen($lname) < 2) {
$error['lname'] = "Please enter your last name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Name
$name = $fname . " " . $lname;
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
$response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>

Categories