Having an Issue with a form field on my site - php

I've got a basic form on my website and have been trying to add in a phone number field.
The header validation is:
//process the contact form
$('#submit-form').click(function(){
var reg = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var names = $('#contact-form [name="contact-names"]').val();
var email_address = $('#contact-form [name="contact-email"]').val();
var phone = $('#contact-form [name="phone"]').val();
var comment = $.trim($('#contact-form #message').val());
var data_html ='' ;
if(names == ""){
$('.name-required').html('Please enter your name.');
}else{
$('.name-required').html('');
}
if(phone == ""){
$('.phone-required').html('Please enter your phone number.');
}else{
$('.phone-required').html('');
}
if(email_address == ""){
$('.email-required').html('Your email is required.');
}else if(reg.test(email_address) == false){
$('.email-required').html('Invalid Email Address.');
}else{
$('.email-required').html('');
}
if(comment == ""){
$('.comment-required').html('Comment is required.');
}else{
$('.comment-required').html('');
}
if(comment != "" && names != "" && reg.test(email_address) != false) {
data_html = "names="+ names + "&phone" + phone + "&comment=" + comment + "&email_address="+ email_address;
//alert(data_html);
$.ajax({
type: 'POST',
url: 'php-includes/contact_send.php',
data: data_html,
success: function(msg){
if (msg == 'sent'){
$('#contact-form [name="contact-names"]').val('');
$('#contact-form [name="contact-email"]').val('');
$('#contact-form #contact-phone').val('');
$('#contact-form #message').val('');
$(window.location = "http://theauroraclinic.com/thank-you.html");
}else{
$('#success').html('<div class="error">Mail Error. Please Try Again!</div>') ;
}
}
});
}
return false;
});
});
</script>
The Actual Form is:
<form id="contact-form" method="post" name="contact-form" class="infield rounded-fields form-preset">
<h3>We respect your privacy. </h3>
<h5> Please be assured that we will not share any information without your prior consent.</h5>
<p><span class="note">All fields are required!</span>
<p><label for="name">Name</label>
<input class="text" name="contact-names" type="text" id="name" />
<span class="name-required"></span></p>
<p><label for="email">Email</label>
<input class="text" name="contact-email" type="text" id="email" />
<span class="email-required"></span></p>
<p><label for="phone">Phone</label>
<input class="text" name="contact-phone" type="text" id="phone" />
<span class="phone-required"></span></p>
<p><label for="message" class="message-label">Message</label>
<textarea class="text-area" name="contact-comment" id="message"></textarea>
<span class="comment-required"></span></p>
<p><input class="send" id="submit-form" name="submit" type="submit" value="Send" /></p>
</form>
And the form processing php file is:
<?php
$names = $_POST['names'];
$email = $_POST['email_address'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
$to ='name#email.com';
$message = "";
$message .= "*Name: " . htmlspecialchars($names, ENT_QUOTES) . "<br>\n";
$message .= "*Email: " . htmlspecialchars($email, ENT_QUOTES) . "<br>\n";
$message .= "*Phone: " . htmlspecialchars($phone, ENT_QUOTES) . "<br>\n";
$message .= "Comment: " . htmlspecialchars($comment, ENT_QUOTES) . "<br>\n";
$lowmsg = strtolower($message);
$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: \"" . $names . "\" <" . $email . ">\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$message = utf8_decode($message); mail($to, "Note from the Contact Form", $message, $headers);
if ($message){
echo 'sent';
}else{
echo 'failed';
}
?>
The form still works fine, but it won't pass any input into the phone number field. I did add in the redirect on submission. It's weird that that would only affect 1 field though. Any help would be great, I'm not that good at js a& php!

The phone field is named contact-phone not phone

In the javascript, [name="phone"] should be changed to [name="contact-phone"].

data_html = "names="+ names + "&phone" + phone + "&comment=" + comment + "&email_address="+ email_address;
change this... &phone= is missing
data_html = "names="+ names + "&phone=" + phone + "&comment=" + comment + "&email_address="+ email_address;

Related

How to verify an input field using php

How can I verify that the contact number a user inputs in the contact form is in fact numbers and they are at least 10 numbers? Meaning I want the input field only accept 10 numbers or more and also accept the plus (+) sign. At the moment the php verifies that the all fields are completed and the email correct.
here is the html code:
<div class="contact-form">
<form id="contact-form" action="sendmail.php" method="post" title="Contact Form" role="form">
<div class="col-sm-6">
<div class="form-group">
<label for="contact-name">Full name</label>
<input type="text" name="name" placeholder="Enter your full name..." class="contact-name" id="contact-name">
</div>
<div class="form-group">
<label for="contact-email">Email</label>
<input type="text" name="email" placeholder="Enter your email..." class="contact-email" id="contact-email">
</div>
<div class="form-group">
<label for="contact-number">Contact number</label>
<input type="text" name="number" class="active" id="contact-number" placeholder="Your contact number...">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="contact-message">Message</label>
<textarea name="message" placeholder="Your message..." class="contact-message" id="contact-message"></textarea>
</div>
</div>
<div class="col-sm-12 text-center">
<button type="submit" class="btn btn-default sketchFlowPrint" id="submit">Send</button>
</div>
</form>
</div>
here is the php code:
<?php
// Email address verification
function isEmail($email) {
return preg_match('|^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]{2,})+$|i', $email);
};
if($_POST) {
// Enter the email where you want to receive the message
$emailTo = 'example#gmail.com';
$clientName = addslashes(trim($_POST['name']));
$clientEmail = addslashes(trim($_POST['email']));
$number = addslashes(trim($_POST['number']));
$message = addslashes(trim($_POST['message']));
$subject = 'Query from My Domain';
$sendMessage = 'Hi' . "\n\n";
$sendMessage .= $message . "\n\n";
$sendMessage .= 'From: ' . $clientName . "\n";
$sendMessage .= 'Email: ' . $clientEmail . "\n";
$sendMessage .= 'Contact number: ' . $number . "\n";
$array = array();
$array['nameMessage'] = '';
$array['emailMessage'] = '';
$array['numberMessage'] = '';
$array['messageMessage'] = '';
if($clientName == '') {
$array['nameMessage'] = 'Please enter your full name.';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Please insert a valid email address.';
}
if($number == '') {
$array['numberMessage'] = 'Please enter a valid contact number.';
}
if($message == '') {
$array['messageMessage'] = 'Please enter your message.';
}
if($clientName != '' && isEmail($clientEmail) && $message != '') {
// Send email
$headers = "From: " . $clientName . ' <' . $clientEmail . '>' . "\r\n";
$headers .= PHP_EOL;
$headers .= "MIME-Version: 1.0".PHP_EOL;
$headers .= "Content-Type: multipart/mixed;".PHP_EOL;
$headers .= " boundary=\"boundary_sdfsfsdfs345345sfsgs\"";
mail($emailTo, $subject, $sendMessage, $headers);
}
echo json_encode($array);
} else {
header ('location: index.html#contact');
}
?>
here is the jQuery:
// Contact form
$('.contact-form form').submit(function(e) {
e.preventDefault();
var form = $(this);
var nameLabel = form.find('label[for="contact-name"]');
var emailLabel = form.find('label[for="contact-email"]');
var numberLabel = form.find('label[for="contact-number"]');
var messageLabel = form.find('label[for="contact-message"]');
nameLabel.html('Full name');
emailLabel.html('Email');
numberLabel.html('Contact number');
messageLabel.html('Message');
var postdata = form.serialize();
$.ajax({
type: 'POST',
url: 'sendmail.php',
data: postdata,
dataType: 'json',
success: function(json) {
if(json.nameMessage !== '') {
nameLabel.append(' - <span class="red error-label"> ' + json.nameMessage + '</span>');
}
if(json.emailMessage !== '') {
emailLabel.append(' - <span class="red error-label"> ' + json.emailMessage + '</span>');
}
if(json.numberMessage !== '') {
numberLabel.append(' - <span class="red error-label"> ' + json.numberMessage + '</span>');
}
if(json.messageMessage !== '') {
messageLabel.append(' - <span class="red error-label"> ' + json.messageMessage + '</span>');
}
if(json.nameMessage === '' && json.emailMessage === '' && json.numberMessage === '' && json.messageMessage === '') {
form.fadeOut('fast', function() {
form.parent('.contact-form').append('<h2 class="text-center"><span class="orange">Thanks for contacting us!</span> We will get back to you very soon.</h2>');
});
}
}
});
});
I'm guessing that the + needs to be the first character (international phone numbers)?
Then this should do it:
if (!preg_match('/^(\+?)+([0-9]{10,})$/', $number)) {
$array['numberMessage'] = 'Please enter a valid contact number.'
}
The plus sign is optional. If it should be required, just remove the ? after the plus sign in the expression.
Check the length is less than 10 or see if the input differs from the filtered input (+ and digits removed).
<?php
function valid_tel($in) {
$valid = true;
if(strlen($in) < 10 || $in !== preg_replace('#[^0-9+]#','', $in)) {
$valid = false;
}
return $valid;
}
var_dump(valid_tel(''));
var_dump(valid_tel('01234'));
var_dump(valid_tel('01234567890')); // 11 long
var_dump(valid_tel('+012345678')); // 10 inc +
var_dump(valid_tel('+0123456'));
var_dump(valid_tel('0123456xyz7890'));
Output:
boolean false
boolean false
boolean true
boolean true
boolean false
boolean false

Not receiving all fields in php mail

Here is the mail handler only part being received in sent emails is the actual message missing parts are name and phone.
<?php
session_start();
$email_to = "test#test.com";
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
if(mail($email_to, $subject, $message, $headers)){
echo "success";
}
else{
echo "failed";
}
?>
Here is the html part which I have checked over and over and cant figure out whats wrong
<form id="contact" class="row" name="form1" method="post" action="">
<div class="span2">
<label>Name <span class="req">*</span></label>
<input type="text" class="full" name="name" id="name"/>
<div id="error_name" class="error">Please check your name</div>
</div>
<div class="span2">
<label>Phone <span class="req">*</span></label>
<input type="text" class="full" name="phone" id="phone"/>
<div id="error_phone" class="error">Please check your phone</div>
</div>
<div class="span4">
<label>Email <span class="req">*</span></label>
<input type="text" class="full" name="email" id="email"/>
<div id="error_email" class="error">Please check your email</div>
</div>
<div class="span8">
<label>Message <span class="req">*</span></label>
<textarea cols="8" rows="10" name="message" id="message" class="full"></textarea>
<div id="error_message" class="error">Please check your message</div>
<div id="mail_success" class="success"> Thank you. Your message has been sent.</div>
<div id="mail_failed" class="error">Error, email not sent</div>
<p id="btnsubmit"><input type="submit" id="send" value="Send" class="btn btn-large"/></p>
</div>
</form>
Js script -------------------
$(document).ready(function(){
$("#send").click(function(){
var name = $("#name").val();
var phone = $("#phone").val();
var email = $("#email").val();
var message = $("#message").val();
var error = false;
if (name.length == 0) {
var error = true;
$("#error_name").fadeIn(500);
}
else {
$("#error_name").fadeOut(500);
}
if (phone.length == 0) {
var error = true;
$("#error_phone").fadeIn(500);
}
else {
$("#error_phone").fadeOut(500);
}
if(email.length == 0 || email.indexOf("#") == "-1" || email.indexOf(".") == "-1"){
var error = true;
$("#error_email").fadeIn(500);
}
else {
$("#error_email").fadeOut(500);
}
if(message.length == 0){
var error = true;
$("#error_message").fadeIn(500);
}else{
$("#error_message").fadeOut(500);
}
if(error == false){
$("#send").attr({"disabled" : "true", "value" : "Loading..." });
$.ajax({
type: "POST",
url : "send.php",
data: "name=" + name + "&email=" + email + "&subject=" + "Website Enquiry" + "&message=" + message,
success: function(data){
if(data == 'success'){
$("#btnsubmit").remove();
$("#mail_success").fadeIn(500);
}else{
$("#mail_failed").html(data).fadeIn(500);
$("#send").removeAttr("disabled").attr("value", "send");
}
}
});
}
return false;
});
});
Obviously, you are missing parts!... You simply declare variables, but you didn't include them in the mail itself.
In this example, I'm adding $name and $phone to $message body.
<?php
session_start();
$email_to = "test#test.com";
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'] . "\n";
$message .= "Name: " . $name . "\n";
$message .= "Phone: " . $phone;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
if(mail($email_to, $subject, $message, $headers)){
echo "success";
}
else{
echo "failed";
}
?>

PHP mail() sends 2 copies

I have this problem with my contact form. When I submit the form I receive 2 identical emails in my box.
Im using JS to check the form for errors and then simple PHP mail() function to send the email.
Here is the PHP code:
<?php
$from = Trim(stripslashes($_POST['email']));
$to = "myemail#gmail.com";
$subject = "Contact Form";
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$number = Trim(stripslashes($_POST['number']));
$message = Trim(stripslashes($_POST['message']));
$body = "";
$body .= "Name: ";
$body .= $name;
$body .= "\n\n";
$body .= "E-mail: ";
$body .= $email;
$body .= "\n\n";
$body .= "Telephone Number: ";
$body .= $number;
$body .= "\n\n";
$body .= "Message: ";
$body .= $message;
$body .= "\n\n";
$success = mail($to, $subject, $body, "From: <$from>" . "\r\n" . "Reply-to: <$from>" . "\r\n" . "Content-type: text; charset=utf-8");
?>
And here is the JS:
$(".submit").click(function() {
var name = $("input[name=name]").val();
var email = $("input[name=email]").val();
var number = $("input[name=number]").val();
var message = $("textarea[name=message]").val();
if (defaults['name'] == name || name == "") {
$(".error").text("Please enter your name!");
return false;
} else if (defaults['email'] == email || email == "") {
$(".error").text("Please enter your email!");
return false;
} else if (defaults['number'] == number || number == "") {
$(".error").text("Please enter your number!");
return false;
} else if (defaults['message'] == message || message == "") {
$(".error").text("Plese enter your message!");
return false;
}
var dataString = 'name=' + name + '&email=' + email + '&number=' + number + '&message=' + message;
$(".error").text("Please wait...").hide().fadeIn("fast");
$.ajax({
type: "POST",
url: "contact.php",
data: dataString,
success: function() {
$('#form form').html("");
$('#form form').append("<div id='success'>Your message has been sent! Thank you</div>");
}
});
return false;
});
And here is the HTML form:
<form id="contact" method="post" action="#">
<label for="name">Name:</label>
<input type="text" name="name" required tabindex="1">
<label for="email">Email adress:</label>
<input type="text" name="email" required tabindex="2">
<label for="number">Tel. number:</label>
<input type="text" name="number" tabindex="3">
<label for="message">Your message:</label>
<textarea name="message" rows="10" cols="70" required tabindex="4"></textarea>
<input type="checkbox" id="terms" name="terms" value="terms">I agree to the terms
<input type="submit" name="submit" class="submit more-info" tabindex="5" value="Send">
<span class="error"></span>
</form>
I have been using the same code for all of my contact forms and it worked all right. Could it be hosting/server related issue?
replace your click event
$(".submit").click(function() {
with
$('.submit').unbind('click').click(function() {
code.
What I can assume your click event is binding two times may be due to a lot of the mess in the code
also use this line in the end of the click event function
$('.submit').unbind('click').click(function() {
// your stuff
event.stopImmediatePropagation(); // as long as not bubbling up the DOM is ok?
});
for reference have a look at the link: https://forum.jquery.com/topic/jquery-executes-twice-after-ajax
$(".submit").click(function(e) { ... }) POSTS to your server for the first time.
Because this is a <submit> button, the form will still submit. The form POSTS to your server for the second time.
The solution would be adding a e.preventDefault() at the bottom inside the $(".submit").click function...
$(".submit").click(function(e) {
// ^ add this e
var name = ...;
$.ajax({
...
});
e.preventDefault();
return false;
});
Try removing the jQuery animations:
$(".error").text("Please wait...").hide().fadeIn("fast");
They can sometimes cause problems.

php redirect thank you page

First I should say I know nothing about php and I'm trying to modify something in my site on my own. I want users to be redirected to a thank you page after they submit the contact form.I have been reading about this subject through all the other answers still I don't get it.
My html code is:
<div id="form_container">
<div id="main">
<p><small>Name</small> <input type="text" name="name" id="name" /></p>
<p><small>Email</small> <input type="text" name="email" id="email" /></p>
<p><small>Phone</small> <input type="text" name="phone" id="phone" /></p>
<p><small>Message</small> <textarea name="message" id="message" rows="12"></textarea></p>
<p><input type="submit" name="submit" id="submit" value="Email Us!" /></p>
<ul id="response" />
</div><!--end main -->
My php code is:
<?php
$name = trim($_POST['name']);
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = stripslashes($_POST['message'] . "<br>Phone Number: " . $_POST['phone']);
$site_owners_email = 'xxxxxxx'; // Replace this with your own email address
$site_owners_name = 'xxxxxxxxx'; // replace with your name
if (strlen($name) < 2) {
$error['name'] = "Your name is.....";
}
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Invalid email address";
}
if (strlen($phone) < 10) {
$error['phone'] = "Your phone number?";
}
if (strlen($message) < 5) {
$error['message'] = "Your message?";
}
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "Website Contact Form";
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->AddAddress('xxxxxxxxxx', 'xxxxxxxx');
$mail->Body = $message
$mail->Send();
echo "<li class='success'> Congratulations, " . $name . ". We received your message</li>";
} # end if no error
else {
$response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
$response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
$response .= (isset($error['phone'])) ? "<li>" . $error['phone'] . "</li> \n" : null;
$response .= (isset($error['message'])) ? "<li>" . $error['message'] . "</li>" : null;
echo $response;
} # end if there was an error sending
?>
My js file is
$(function() {
var paraTag = $('input#submit').parent('p');
$(paraTag).children('input').remove();
$(paraTag).append('<input type="button" name="submit" id="submit" value="Email Us Now!" />');
$('#main input#submit').click(function() {
$('#main').append('<img src="img/ajax-loader.gif" class="loaderIcon" alt="Loading..." />');
var name = $('input#name').val();
var email = $('input#email').val();
var phone = $('input#phone').val();
var message = $('textarea#message').val();
$.ajax({
type: 'post',
url: 'sendEmail.php',
data: 'name=' + name + '&email=' + email + '&phone=' = phone + '&comments=' + comments,
success: function(results) {
$('#main img.loaderIcon').fadeOut(1000);
$('#response').html(results);
}
}); // end ajax
});
});
All I want is users to see a simple thank you page after they fill out the form.
Assuming the PHP code is invoked using AJAX, you could add this line to the success part of the AJAX request.
window.location.href = '/path/to/your/thankyou/page.html';
only if the results from the sendEmail.php page suggests the message was sent. One way to do this is to make the sendEmail.php echo "OK" only if the message was sent. Then you can do something like this:
success: function(results) {
$('#main img.loaderIcon').fadeOut(1000);
if (results == "OK") {
window.location.href = '/path/to/your/thankyou/page.html';
}
}
To make sendEmail.php only return "OK" if a message was sent, change this line:
echo "<li class='success'> Congratulations, " . $name . ". We received your message</li>";
with this:
echo "OK";
If the form is validated correctly in php and no errors are found, you need to add this line of code to php
echo "<script>window.location="http://www.location.com/other.htm"</script>";
so that this string is returned on successful submission of form to the ajax function and on execution the user will be redirected to the next location.

Form submit is not showing success message

I am having a problem with my contact form where there is no success indication being given, even though the message is successfully sent to email. The form appears broken to the user because the page is unchanged when the submit button is pressed. This page was built using a Themeforest template, but the author is unable to provide a solution. I was hoping that someone could point me in the right direction.
Here is the stock 'contact-send.php' file I was given:
<?php
$names = $_POST['names'];
$email = $_POST['email_address'];
$comment = $_POST['comment'];
$to ='to#email.com';
$message = "";
$message .= "*Name: " . htmlspecialchars($names, ENT_QUOTES) . "<br>\n";
$message .= "*Email: " . htmlspecialchars($email, ENT_QUOTES) . "<br>\n";
$message .= "*Comments: " . htmlspecialchars($comment, ENT_QUOTES) . "<br>\n";
$lowmsg = strtolower($message);
$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: \"" . $names . "\" <" . $email . ">\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$message = utf8_decode($message); mail($to, "Note from the Contact Form", $message, $headers);
if ($message){
echo 'sent';
}else{
echo 'failed';
}
?>
This is the stock html code:
<form id="contact-form" name="contact-form" action="" method="post">
<label class="contact-label">Name*</label>
<input class="contact-input" type="text" name="contact-names" value="" /><br /><span class="name-required"></span>
<label class="contact-label">Email*</label>
<input class="contact-input" type="text" name="contact-email" value="" /><br /><span class="email-required"></span>
<label class="contact-label">Message*</label>
<textarea name="comments" rows="2" cols="20" class="contact-commnent"></textarea><br /><span class="comment-required"></span>
<input type="submit" value="Send" id="submit-form" class="button lightred contact-submit" />
</form>
This file is attached to the html pages as well: (juery-contact.js)
$(document).ready(function(){
$('#submit-form').click(function(){
var reg = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var names = $('#contact-form [name="contact-names"]').val();
var email_address = $('#contact-form [name="contact-email"]').val();
var comment = $.trim($('#contact-form .contact-commnent').val());
var data_html ='' ;
if(names == ""){
$('.name-required').html('Your name is required.');
}else{
$('.name-required').html('');
}
if(email_address == ""){
$('.email-required').html('Your email is required.');
}else if(reg.test(email_address) == false){
$('.email-required').html('Invalid Email Address.');
}else{
$('.email-required').html('');
}
if(comment == ""){
$('.comment-required').html('Comment is required.');
}else{
$('.comment-required').html('');
}
if(comment != "" && names != "" && reg.test(email_address) != false){
data_html = "names="+ names + "&comment=" + comment + "&email_address="+ email_address;
//alert(data_html);
$.ajax({
type: 'post',
url: 'contact-send.php',
data: data_html,
success: function(msg){
if (msg == 'sent'){
$('#success').html('Message sent!') ;
$('#contact-form [name="contact-names"]').val('');
$('#contact-form [name="contact-email"]').val('');
$('#contact-form .contact-commnent').val('');
}else{
$('#success').html('Mail Error. Please Try Again.!') ;
}
}
});
}
return false;
})
})
Here is the live site if you need it: http://shamrockmasonry.ca/contact.html
Any help would be appreciated!
I glanced at the source code and noticed that there isn't an element with '#success' id.
So this block of code that adds html message to #success element can't do that because that element does not actually exist:
if (msg == 'sent'){
$('#success').html('Message sent!');
$('#contact-form [name="contact-names"]').val('');
$('#contact-form [name="contact-email"]').val('');
$('#contact-form .contact-commnent').val('');
}else{
$('#success').html('Mail Error. Please Try Again.!') ;
}
To answer your additional question you made in the comments about removing the error messages you got after successful submit do the following.
Add additional class to your span's, like in example here
<span class="email-required error-message"></span>
<span class="name-required error-message"></span>
<span class="comment-required error-message"></span>
And add this line of code to your jquery block:
if (msg == 'sent'){
$('#success').html('Message sent!');
$('#contact-form [name="contact-names"]').val('');
$('#contact-form [name="contact-email"]').val('');
$('#contact-form .contact-commnent').val('');
$('.error-message').empty(); // clears all of the current error messages on success
}else{
$('#success').html('Mail Error. Please Try Again.!') ;
}
So add a div element with #success id above your form tag and you should be good.
Hope it helps.
Add <div id="success"></div> before form HTML
Should be:
<div id="success"></div>
<form id="contact-form" name="contact-form" action="" method="post">
[...]
</form>

Categories