I am trying to add another validation to a contact form. I need a checkbox at the bottom of the form which the user check before they can submit the form.
This is what i have so far, which validates the form. I would like to know how to add a checkbox so the form is only submitted if the box is checked
<section class="form wow fadeInRight" data-wow-duration="2s">
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 contact-form">
<h2>Contact Us</h2>
<p>Please do not hesitate to get in touch with us by filling out the form or using the contact details below</p>
<form class="form-horizontal" action="" id="form" method="post" name="form">
<input type="hidden" name="bts" value="" />
<input type="hidden" name="page" value="<?php echo $current_url; ?>" />
<div class="form-group">
<div class="col-md-12">
<input type="text" class="form-control" id="name" name="name" placeholder="Your Name:*" value="<?php echo $send_data['name']; ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address:*" value="<?php echo $send_data['email']; ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="tel" minlength="10" class="form-control" id="tel" name="tel" placeholder="Contact Number:*" value="<?php echo $send_data['tel']; ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<textarea class="form-control" id="message" name="message" placeholder="General Enquiry:" required><?php echo $send_data['message']; ?></textarea>
</div>
</div>
<div class="form-group">
<div class=" col-md-12">
<button type="submit" class="button btn btn-warning">Send</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<?php
$target_page = false;
$send_to = config('company_email');
$company_name = config('company_name');
$errors = array();
$posted = false;
$success = false;
$current_url = (isset($_POST['page']) && !empty($_POST['page'])) ? $_POST['page'] : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
# is this the normal or quick form?
$quick = (isset($_POST['type']) && $_POST['type'] == 'quick') ? true : false;
# set up the common fields
$send_data = array();
$send_data['name'] = (isset($_POST['name']) && !empty($_POST['name'])) ? $_POST['name'] : '';
$send_data['email'] = (isset($_POST['email']) && !empty($_POST['email'])) ? $_POST['email'] : '';
# if this is not the quick form then add the additional fields
if (!$quick) {
$send_data['tel'] = (isset($_POST['tel']) && !empty($_POST['tel'])) ? $_POST['tel'] : '';
}
$send_data['message'] = (isset($_POST['message']) && !empty($_POST['message']) && $_POST['message'] != ' ') ? $_POST['message'] : '';
# check if the form has been submitted
if (isset($_POST['bts']) && $_POST['bts'] == '') {
# the form has been posted
$posted = true;
# check the name value
if ($send_data['name'] == '') {
$errors[] = 'Please fill in your name';
}
# check the email value
if ($send_data['email'] == '') {
$errors[] = 'Please fill in your email address';
} else {
# validate the email
$validate_email = filter_var($send_data['email'], FILTER_VALIDATE_EMAIL);
if (!$validate_email) {
$errors[] = 'Please check your email address is correct';
} else {
# check the domains MX records
if(!checkdnsrr(array_pop(explode("#",$send_data['email'])),"MX")){
$errors[] = 'Please use a valid email address';
}
}
}
# if this is not the quick form then check the additional fields
if (!$quick) {
if (strlen($send_data['tel']) < 10) {
$errors[] = 'Please add a valid telephone number!';
}
}
# check the message
if ($send_data['message'] == '') {
$errors[] = 'Please fill in a message';
}
# if there are no errors then send the message
if (count($errors) == 0) {
$send_from = $send_data['name'] . "<" . $send_data['email'] . ">";
$subject = 'Enquiry From Website ' . $_SERVER['HTTP_HOST'];
$email_message = 'Below are the details that have been submitted on your contact form' . "\n\n";
$email_message .= '________________________________________' . "\n\n";
if (count($send_data) > 0) {
foreach ($send_data as $key => $value) {
$email_message .= $key . ' : ' . htmlspecialchars($value) . "\n";
}
}
$email_message .= '________________________________________' . "\n\n";
$email_message .= 'IP : ' . $_SERVER["REMOTE_ADDR"] . "\n\n";
$email_message .= 'URL : ' . $current_url . "\n\n";
$email_message .= 'WUKmedia | http://wukmedia.uk';
#$headers = "From: " . strip_tags($send_from) . "\r\n";
#$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
#$headers .= "MIME-Version: 1.0\r\n";
#$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($send_to, $subject, $email_message, "From: " . $send_from)) {
$success = true;
# now that the enquiry has been sent we can confirm to user
$FromCompany = $company_name . "<" . $send_to . ">";
$thanks_email = "Thank you for your enquiry " . $send_data['name'] . ".\n";
$thanks_email .= "We will be back in touch with you shortly, \n\n";
$thanks_email .= $company_name . "\n";
$thanks_email .= 'http://' . $_SERVER['HTTP_HOST'] . "\n\n\n";
mail($send_data['email'], "Thanks for the Enquiry", $thanks_email, "From: " . $send_from);
# now store the data in a json array
$send_data['ip'] = $_SERVER["REMOTE_ADDR"];
$send_data['timestamp'] = time();
$json_array = "\n" . json_encode($send_data);
$json_file = $_SERVER['DOCUMENT_ROOT'] . '/tp/enquiries/enquiries.json';
file_put_contents($json_file, $json_array, FILE_APPEND | LOCK_EX);
} else {
$errors[] = 'Your enquiry has not been sent, please try again';
}
}
}
$target_page = false;
$send_to = config('company_email');
$company_name = config('company_name');
$errors = array();
$posted = false;
$success = false;
$current_url = (isset($_POST['page']) && !empty($_POST['page'])) ? $_POST['page'] : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
# is this the normal or quick form?
$quick = (isset($_POST['type']) && $_POST['type'] == 'quick') ? true : false;
# set up the common fields
$send_data = array();
$send_data['name'] = (isset($_POST['name']) && !empty($_POST['name'])) ? $_POST['name'] : '';
$send_data['email'] = (isset($_POST['email']) && !empty($_POST['email'])) ? $_POST['email'] : '';
# if this is not the quick form then add the additional fields
if (!$quick) {
$send_data['tel'] = (isset($_POST['tel']) && !empty($_POST['tel'])) ? $_POST['tel'] : '';
}
$send_data['message'] = (isset($_POST['message']) && !empty($_POST['message']) && $_POST['message'] != ' ') ? $_POST['message'] : '';
# check if the form has been submitted
if (isset($_POST['bts']) && $_POST['bts'] == '') {
# the form has been posted
$posted = true;
# check the name value
if ($send_data['name'] == '') {
$errors[] = 'Please fill in your name';
}
# check the email value
if ($send_data['email'] == '') {
$errors[] = 'Please fill in your email address';
} else {
# validate the email
$validate_email = filter_var($send_data['email'], FILTER_VALIDATE_EMAIL);
if (!$validate_email) {
$errors[] = 'Please check your email address is correct';
} else {
# check the domains MX records
if(!checkdnsrr(array_pop(explode("#",$send_data['email'])),"MX")){
$errors[] = 'Please use a valid email address';
}
}
}
# if this is not the quick form then check the additional fields
if (!$quick) {
if (strlen($send_data['tel']) < 10) {
$errors[] = 'Please add a valid telephone number!';
}
}
# check the message
if ($send_data['message'] == '') {
$errors[] = 'Please fill in a message';
}
# if there are no errors then send the message
if (count($errors) == 0) {
$send_from = $send_data['name'] . "<" . $send_data['email'] . ">";
$subject = 'Enquiry From Website ' . $_SERVER['HTTP_HOST'];
$email_message = 'Below are the details that have been submitted on your contact form' . "\n\n";
$email_message .= '________________________________________' . "\n\n";
if (count($send_data) > 0) {
foreach ($send_data as $key => $value) {
$email_message .= $key . ' : ' . htmlspecialchars($value) . "\n";
}
}
$email_message .= '________________________________________' . "\n\n";
$email_message .= 'IP : ' . $_SERVER["REMOTE_ADDR"] . "\n\n";
$email_message .= 'URL : ' . $current_url . "\n\n";
$email_message .= 'WUKmedia | http://wukmedia.uk';
#$headers = "From: " . strip_tags($send_from) . "\r\n";
#$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
#$headers .= "MIME-Version: 1.0\r\n";
#$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($send_to, $subject, $email_message, "From: " . $send_from)) {
$success = true;
# now that the enquiry has been sent we can confirm to user
$FromCompany = $company_name . "<" . $send_to . ">";
$thanks_email = "Thank you for your enquiry " . $send_data['name'] . ".\n";
$thanks_email .= "We will be back in touch with you shortly, \n\n";
$thanks_email .= $company_name . "\n";
$thanks_email .= 'http://' . $_SERVER['HTTP_HOST'] . "\n\n\n";
mail($send_data['email'], "Thanks for the Enquiry", $thanks_email, "From: " . $send_from);
# now store the data in a json array
$send_data['ip'] = $_SERVER["REMOTE_ADDR"];
$send_data['timestamp'] = time();
$json_array = "\n" . json_encode($send_data);
$json_file = $_SERVER['DOCUMENT_ROOT'] . '/tp/enquiries/enquiries.json';
file_put_contents($json_file, $json_array, FILE_APPEND | LOCK_EX);
} else {
$errors[] = 'Your enquiry has not been sent, please try again';
}
}
}
just put required on tag checkbox. ex:
<input type="checkbox" name="" value="" required> text
When validating the forms I code, I just use the Recaptcha. Is this an option for you? If it is then you need the site to be live so you have access to the domain name of the site.
To begin this go to: https://www.google.com/recaptcha/intro/v3beta.html
Get a SITEKEY & SECERTKEY - they have step by step (very simple) on how to access these two keys.
Once you have them only a small snippet of code in the head tags like so:
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
After, you just have to add the code into your form tags like so:
<form>
<!-- put this wherever you want it in the form - usually at the bottom -->
<div class="g-recaptcha" data-sitekey="<!-- Site Key HERE -->"></div>
</form>
And then finally just authorise it in your PHP code that handles the form data, for example:
$captcha = $_POST["g-recaptcha-response"]; // Declare variable
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
header("Location: <!-- Error HTML page OR echo and error statement -->");
exit;
}
$secretKey = "<!-- SECRET KEY HERE -->";
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response, true);
if(intval($responseKeys["success"]) !== 1) {
echo '<h2>Spam Detected</h2>';
}
else {
header("Location: <!-- Thank you HTML page OR echo and thanks statement -->");
}
You'll see I've commented where the site & secret key go.
Related
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
}
?>
...................................................................
I have a problem that after I fill out the contact form on my HTML website I receive over 50 same E-mails. Its a HTML form connected to contact.php file which code is shown bellow. I have set everything but maybe there is a problem in my code or somewhere else.
My code is over here
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+ (ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Vyplnte meno.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Vyplnte email.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Zadali ste nesprávny e-mail, skúste to znovu.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">Vyplnte text správy.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
$address = "noreply#marcelaskolenia.sk";
$address = "lubosmasura#gmail.com";
$toCustomer = $email;
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Mate novu spravu od ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "Mate novu spravu od $name." . PHP_EOL . PHP_EOL;
$e_content = "\"$subject\"" . "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "Kontaktujte $name cez email, $email alebo cez mobil $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers .= 'To: Test <noreply#marcelaskolenia.sk>' . "\r\n";
$headers .= 'From: Testk <noreply#marcelaskolenia.sk>' . "\r\n";
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h3 class'mark'>Sprava bola odoslana.</h3>";
echo "<p>Dakujeme <strong>$name</strong>, Vasa sprava nam bude dorucena.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
HTML code
<div class="contact_form">
<div id="message"></div>
<form id="contactform" class="row" action="contact.php" name="contactform" method="post">
<div class="col-md-12">
<input type="text" name="name" id="name" class="form-control" placeholder="Meno">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Telefónne číslo">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Predmet">
<textarea class="form-control" name="comments" id="comments" rows="6" placeholder="Text správy"></textarea>
<button type="submit" value="SEND" id="submit" class="btn btn-primary"> ODOSLAŤ</button>
</div>
</form>
</div>
</div><!-- end col -->
Any Ideas why is this happening?
Thank you.
I have the following HTML:
<form method="post" action="https://www.domain.co.uk/v2/contact.php" id="contactform">
<p><label for="name"><span class="required">*</span> Your Name</label><br>
<input name="name" type="text" id="name" size="30" value="" class="input" required> </p>
<p><label for="email"><span class="required">*</span> Email</label><br>
<input name="email" type="email" id="email" size="30" value="" class="input" required></p>
<p><label for="phone">Phone</label><br>
<input name="phone" type="tel" id="phone" size="30" value="" class="input"></p>
<p><label for="subject">Subject</label><br>
<select name="subject" id="subject" class="input">
<option value="Not sure" selected="selected">Not sure</option>
<option value="this">this</option>
<option value="that">that</option>
</select></p>
<p><label for="comments"><span class="required">*</span> Message</label><br>
<textarea name="comments" cols="40" rows="3" id="comments" class="input" required></textarea></p>
<p><input type="submit" class="submit" id="submit" value="Submit"></p>
</form>
and the following in contact.php
<?php
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['phone'] != "") {
$_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
if ($_POST['phone'] == "") {
$errors .= 'Please enter your phone number.<br/>';
}
} else {
$errors .= 'Please enter your phone number.<br/>';
}
if ($_POST['subject'] != "") {
$_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
if ($_POST['subject'] == "") {
$errors .= 'Please choose a subject.<br/>';
}
} else {
$errors .= 'Please choose a subject.<br/>';
}
if ($_POST['comments'] != "") {
$_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
if ($_POST['comments'] == "") {
$errors .= 'Please enter a message.<br/>';
}
} else {
$errors .= 'Please enter a message.<br/>';
}
if (!$errors) {
$mail_to = 'info#myemails.co.uk';
$subject = 'Enquiry';
$message .= 'Regarding: ' . $_POST['subject'] . "\n\n";
$message .= 'Name: ' . $_POST['name'] . "\n\n";
$message .= 'Email: ' . $_POST['email'] . "\n\n";
$message .= 'Phone: ' . $_POST['phone'] . "\n\n";
$message .= 'Message ' . $_POST['comments'] . "\n\n";
$success = mail($mail_to, $subject, $message, "From: <$email>");
if ($success){
print "sent";
}
else{
print "failed";
}
}
?>
no matter what I change or try I end up on a blank white page for contact.php instead of seeing the sent or failed message (having removed my javascript validation incase I was causing issue there), likewise there is nothing in the error log and despite having gone back over the code I can't spot the issue? Unsure if I have stared at it for too long and missing something obvious or there is a deeper problem?
Any pointers appreciated.
var_dump shows it is getting the information:
array(5) { ["name"]=> string(10) "Joe Bloggs" ["email"]=> string(16) "joe#anyemail.com" ["phone"]=> string(11) "07123456789" ["subject"]=> string(8) "Not sure" ["comments"]=> string(17) "test message here" }
You used string concatenation, but you didn't defined your variables before that, if you change your code, like this:
<?php
$errors = '';
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['phone'] != "") {
$_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
if ($_POST['phone'] == "") {
$errors .= 'Please enter your phone number.<br/>';
}
} else {
$errors .= 'Please enter your phone number.<br/>';
}
if ($_POST['subject'] != "") {
$_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
if ($_POST['subject'] == "") {
$errors .= 'Please choose a subject.<br/>';
}
} else {
$errors .= 'Please choose a subject.<br/>';
}
if ($_POST['comments'] != "") {
$_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
if ($_POST['comments'] == "") {
$errors .= 'Please enter a message.<br/>';
}
} else {
$errors .= 'Please enter a message.<br/>';
}
$message = '';
if (empty($errors)) {
$mail_to = 'info#myemails.co.uk';
$subject = 'Enquiry';
$message .= 'Regarding: ' . $_POST['subject'] . "\n\n";
$message .= 'Name: ' . $_POST['name'] . "\n\n";
$message .= 'Email: ' . $_POST['email'] . "\n\n";
$message .= 'Phone: ' . $_POST['phone'] . "\n\n";
$message .= 'Message ' . $_POST['comments'] . "\n\n";
$success = mail($mail_to, $subject, $message, "From: <$email>");
if ($success){
print "sent";
}
else{
print "failed";
}
} else {
echo $errors;
}
?>
It works. You said that you are at live site, so probably errors are not show up, if you want you can add this lines:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
at the top of your php file, to see errors, but clients also would see these errors. You can add IP Check for your IP around them.
Using a small contact form, however when the form is submitted/sent, I'm getting a "No data" message.
<form method="post" action="mail.php">
<input type="text" placeholder="Name*" name="name" required>
<input type="email" placeholder="Email*" name="email" required>
<input type="text" placeholder="Subject" name="subject">
<textarea placeholder="Message" name="message" required></textarea>
<input type="submit" value="Send" name="submit">
</form>
The PHP:
<?php
// variable
$fromemail = 'any_site#my_site_com'; // from mail
$to = "marygsheehan#yahoo.ie"; // to mail
//
// check data
if (!isset($_POST["fields"])) {
die("No data");
}
$fields = $_POST["fields"];
if( empty($fields['name']) ) {
die("No name");
}
if( empty($fields['email']) ) {
die("No email");
}
if (!empty( $fields['code'] ) ) {
die("ok");
}
$subject = "Site mail: " . $fields['subject'];
// subject massege
$subject = '=?utf-8?Q?'."\"".urlencode($subject)."\"".'?=';
$subject= str_replace("%","=",$subject);
$subject = str_replace("+","_",$subject);
// content massage
$name = $name ? $name : 'unknown';
$from = 'Mail from'."<".$fromemail.">";
$mess = $mess ? $mess : 'unknown';
$message = "<b>Client name: </b> " . $fields['name'] . "<br>";
$message .= "<b>Client email: </b> " . $fields['email'] . "<br>";
/*$message .= "<b>Client phone: </b> ".$site."<br>";*/
$message .= "<b>Subject: </b> " . $fields['subject'] . "<br>";
$message .= "<b>Text:</b>\n" . $fields['text'] . "<br>";
$message .= "Sent: ".strftime("%a, %d %b %Y %H:%M:%S");
// end content massage
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Site Mail <" . $fromemail . ">\r\n";
if(mail($to, $subject, $message, $headers)){
print 'ok';
} else {
print 'email not senta';
}
?>
I've been staring at it so long it's a blur, so it's probably a silly mistake. Any help appreciated?
There is no input field in the form with name fields
You need to access them directly like this
$_POST['your_input_name_declared_in_form']
Do it as follow
if(isset($_POST['submit']))
{
$fields = $_POST;
if( empty($fields['name']) ) {
die("No name");
}
if( empty($fields['email']) ) {
die("No email");
}
if (!empty( $fields['code'] ) ) {
die("ok");
}
}
I've set up my form and I'm unsure why the email is being sent and received, but does not include the 'message' field.
I have tried changing the ID's and testing different options but it doesn't seem to send the message
I had the issue a while back with another website but I was able to fix it and I don't remember how.
The reference used is $comment and I've used id=comment so I'm unsure why it's not sending it! Any help much appreciated. I've read other posts on here and no one has a similar issue from what I know.
Here is my website code:
<!-- Form -->` `
<form method="post" action="/contact.php" name="contactform" id="contactform" class="form form-stacked c-form">
<fieldset>
<input name="name" type="text" id="name" placeholder="Your Name" />
<input name="email" type="text" id="email" placeholder="Your E-mail" />
<textarea name="comment" id="comment" placeholder="Message"></textarea>
<input name="verify" type="text" id="verify" size="4" value="" placeholder="3 + 1 =" />
<input type="submit" class="submit btn outline" id="submit" value="Send message" />
</fieldset>
</form>
</div>
Here is my contact form .php
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
//$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Attention! You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
exit();
} else if(trim($comment) == '') {
echo '<div class="error_message">Attention! Please enter a message.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
exit();
}
/*
if(trim($subject) == '') {
echo '<div class="error_message">Attention! Please enter a subject.</div>';
exit();
} else if(trim($comment) == '') {
echo '<div class="error_message">Attention! Please enter your message.</div>';
exit();
} else if(!isset($verify) || trim($verify) == '') {
echo '<div class="error_message">Attention! Please enter the verification number.</div>';
exit();
} else if(trim($verify) != '4') {
echo '<div class="error_message">Attention! The verification number you entered is incorrect.</div>';
exit();
}
*/
if(get_magic_quotes_gpc()) {
$comment = stripslashes($comment);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
$address = "support#idomain.sx";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Email from: ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comment\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email or via phone $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<strong class=\"success\">Email Sent Successfully.</strong>";
echo "<p>Thank you <strong>$name</strong>, please allow up to 5 business days for a response.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
Your textarea is named comments
<textarea name="comments" id="comments" placeholder="Message"></textarea>
But here it's looking for comment
$comment = $_POST['comment'];
Make sure the name is the same as the $_POST variable
Remember form data is appended to the name of the input, not the id
EDIT
$e_content = "\"$comment\"" . PHP_EOL . PHP_EOL;
You aren't concatenating your string and variable. Change it to:
$e_content = $comment . PHP_EOL . PHP_EOL;
I don't know what the backslashes are for. If they are required, change it to:
$e_content = "\" . $comment . "\" . PHP_EOL . PHP_EOL;
Change the $comment = $_POST['comment'];
into
$comment = $_POST['comments'];
Your textarea is named comments
<textarea name="comments" id="comments" placeholder="Message"></textarea>
But here it is looking for comment
$comment = $_POST['comment'];
Make sure the name is the same as the $_POST variable. Remember that form data is appended to the name of the input, not the id.
Edit:
$e_content = "\"$comment\"" . PHP_EOL . PHP_EOL;
You aren't concatenating your string and variable. Change it to:
$e_content = $comment . PHP_EOL . PHP_EOL;
I don't know what the backslashes are for. If they are required, change it to:
$e_content = "\" . $comment . "\" . PHP_EOL . PHP_EOL;