Each time, I submit my form, the expected reset function is not clearing my form. Please how should I clear my form my codes(HTML, PHP, JS) follows below.
HTML codes:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
Javascript code is below:
jQuery(function($) {'use strict',
......
......
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
var form_status = $('<div class="form_status"></div>');
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: formData,
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
$("#main-contact-form")[0].reset();
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
});
});
......
......
});
And last of all is my PHP code:
<?php
$status = array(
'type'=>'success',
'message'=>'Thank you for contacting us. As soon as possible we will contact you!'
);
$fail = array(
'type'=>'fail',
'message'=>'Please try again. Your mail could not be delivered!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email#mail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
header('Content-type: application/json');
if($success){
echo json_encode($status);
}
else{
echo json_encode($fail);
}
?>
Try this code
$("#main-contact-form")[0].reset();
One important thing i was actually missing was that I actually used the reset function before I outputted the obtained message from the server.Looking at my code above, one will discover that i have in my javascipt code the line below:
.done(function(data){
$("#main-contact-form")[0].reset();
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
});
});
which I corrected to become this one below:
.done(function(data){
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
$('#main-contact-form')[0].reset();
//$('#main-contact-form').trigger("reset");
});
A BIG THANKS TO YOU GUYS!
Related
HTML:
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email Address" required="required">
</div>
</div>
</div>
<div class="form-group">
<input type="tel" name="subject" class="form-control" placeholder="Phone" required="required">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required="required"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn-submit">Send Now</button>
</div>
</form>
PHP:
<?php
$mail='kenhaki#gmail.com';
$nombre = $_REQUEST['name'];
$telefono = $_REQUEST['subject'];
$email = $_REQUEST['email'];
$mensaje = $_REQUEST['message'];
$headers = 'From: ccontacto#nhtt.com.mx' . "\r\n" .
'Reply-To: cContacto#nhtt.com.mx' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$thank="gracias.html";
$message = "
nombre15: $nombre
telefono: $telefono
email: $email
mensaje: $mensaje";
if (mail($mail,"Formulario de Contacto Nuevo",$message,$headers))
Header ("Location: $thank");
?>
JS:
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});
I'm a beginner, I have been searching through similar questions but haven't found an explanation of why it won't get the fields from the html form.
I have tried using $_REQUEST and $_POST but none work. I have the same form in another part of the same site where it works fine, maybe here the JS is blocking it somehow?
Thanks in advance for your patience.
You're overriding the default form action with event.preventDefault(). Then all your AJAX call is doing, is requesting the contents of sendemail.php - it's not sending any data to it!
Try altering your JS code to this:
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
method: 'post',
data: $(this).serialize(),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});
I need to send the mail in wordpress for the custom ajax form. The mail
should contain full name, email, phone number and the
message(comment). Please help me to do it.
I have included the html code as a shortcode in functions.php .
function send_grivence(){
var form1 = jQuery("#myform").serialize();
console.log(form1);
jQuery.ajax({
data: {action: 'send_form', form:form1},
type: 'post',
url: 'mydomain.com/wp-admin/admin-ajax.php/',
success: function(data) {
console.log(data); //should print out the name since you sent it along
}
});
}
<form class="form-horizontal" id="myform">
<fieldset>
<div class="control-group">
<!-- Username -->
<label class="control-label" for="fullname">Fullname:</label>
<div class="controls">
<input type="text" id="fullname" name="fullname" placeholder="your-name" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="email">Email Id:</label>
<div class="controls">
<input type="email" id="email" name="email" placeholder="your-email" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="number">Phone No:</label>
<div class="controls">
<input type="text" id="phonenumber" name="phonenumber" placeholder="your-phone no" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="number">Comment:</label>
<div class="controls">
<textarea name="name" rows="8" cols="80" placeholder="your-message"></textarea>
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<a onclick="send_form()" class="btn btn-success">Submit</a>
</div>
</div>
</fieldset>
</form>
first jquery code to submit form via ajax
jQuery(document).ready(function (jQuery) {
jQuery('#myform').submit(contactSubmit);
function contactSubmit() {
var contactForm = jQuery('#myform').serialize();
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: contactForm,
success: function (data) {
// console.log(id);
console.log(data);
}
});
return false;
}
});
make sure that you place a action input in your form like this
<input type="hidden" name="action" value="contactForm"/>, just paste it before submit button. Note that contactForm function is called in function.php
then place a function in your function.php
function contactForm() {
$name = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phonenumber'];
$message = $_POST['comment'];
$quote = 'Full Name: ' . $name . "\n\r" . 'Email: ' . $email . "\n\r" . 'Phone: ' . $phone . "\n\r" . 'Message: ' . $message . "\n\r";
$to = "YOUR EMAIL ADDRESS"; // put your email here
$headers = 'From:' . $name . '<' . $email . '>' . "\r\n"; // put user's email here or duplicate your email
$subject = 'Contact Form';
if ( wp_mail( $to, $subject, $quote, $headers ) === false ) {
echo "Error";
} else {
echo "<h3> Mail Sent Successfully Done</h3>";
}
die();
}
add_action( 'wp_ajax_contactForm', 'contactForm' );
add_action( 'wp_ajax_nopriv_contactForm', 'contactForm' );
Also make sure you replace your email in "YOUR EMAIL ADDRESS". also, make sure your url: "/wp-admin/admin-ajax.php", displays your full address. In local you might include url: "/YOUR_FOLDER_HERE/wp-admin/admin-ajax.php", else in live server it works fine.
EDIT
<textarea name="name" rows="8" cols="80" placeholder="your-message"></textarea>
change name="name" to name="comment"
Hope this helps.
I'm using this script to send an e-mail from a contact form. I get the e-mail but the infos are empty, i tried "echoing" the post values but it won't, what am I missing?
I think it's probably something wrong in my HTML form but I can't see it.
sendemail.php
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$nome = addslashes($_POST['name']);
$email = addslashes($_POST['email']);
$tel = addslashes($_POST['telephone']);
$subject = addslashes($_POST['subject']);
$msg = addslashes($_POST['message']);
$para = "*******#gmail.com";
$body = "Nome: " .$nome. "\nE-mail: " .$email. "\nTelefone: " .$tel. "\nMensagem: " .$msg;
$header = "From: contact#*******.com.br"."\r\n"."Reply-To: ".$email."\r\n"."X-Mailer: PHP/".phpversion();
$success = mail($para, $subject, $body, $header);
echo json_encode($status);
form page html
<section id="contact-page">
<div class="container">
<div class="center">
<h2>Deixe sua mensagem</h2>
</div>
<div class="row contact-wrap">
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Nome *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Telefone</label>
<input type="phone" name="telephone"class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Assunto *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Mensagem *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Enviar</button>
</div>
</div>
</form>
</div>
</div>
</section>
AJAX in JS:
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
});
});
By default, ajax is sent via the GET method. This JavaScript code does not specify POST method; so although the PHP script was called, it never received any data.
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
});
});
The updated script would look something like this (see jQuery AJAX submit form):
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
type: "POST",
data: form.serialize(), // serializes the form's elements.
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
});
});
I am working on my personal website at the moment, and I have done everything, except when a message comes through from a user via the contact form, and email is successfully sent to me, but the content contained within said email is blank.
My code is below:
PHP (sendmail.php)
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Your email has been succesfully sent.'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'me#joshuaquinlan.co.uk';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
HTML (contact-us.html):
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendmail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Full Name">
</div>
<div class="form-group">
<input type="text" name="email" class="form-control" required="required" placeholder="Email address">
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" required="required" placeholder="Subject">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</div>
<div class="col-sm-7">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
</div>
</div>
When the email is actually sent, however, it actually shows up like
this
Please, if someone can help me, then thank you!
Your issue is you are not sending anything from your form:
$.post($(this).attr('action'),function(data){$this.prev().text(data.message) // ...
You forgot to add the data:
$.post($(this).attr('action'),$(this).serialize(), function(data){$this.prev().text(data.message) // ...
Beautified Code
// You forgot to add data here v----
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
}, 'json');
So correct it to:
// added data using serialize() vvvvvvvvvvvvvvvvvv
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
}, 'json');
Find and Replace
Press Ctrl + H (Replace).
Paste this in Find For: $.post($(this).attr('action'),func.
Paste this in Replace With: $.post($(this).attr('action'),$(this).serialize(),func.
Press Replace.
See below screenshot for more details.
Screenshot
I have a JSON code in a sendemail.php file for my Bootstrap 3 contact form. The form is able to send an email to the specified email address but there is no data being submitted. I get blank emails with labels and the header 'unknown sender'. Please help solve this.
This is the bootsrap form code chunk
<div class="col-sm-6">
<h1>Contact Form</h1>
<p>Please contact us using the form or our contact details are available here if you'd like to contact us via Email or Phone.</p>
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="Name" class="form-control" required placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="Email" class="form-control" required placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="Message" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
</div>
</div>
</div>
</form>
</div><!--/.col-sm-6-->
This is the main.js code where the AJAX for contact form is present
jQuery(function($) {
$(function(){
$('#main-slider.carousel').carousel({
interval: 5000,
pause: false
});
});
//Ajax contact
var form = $('.contact-form');
form.submit(function () {
$this = $(this);
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
//smooth scroll
$('.navbar-nav > li').click(function(event) {
if(!$( this ).attr( 'href' ).match(/^#/)) return;
event.preventDefault();
var target = $(this).find('>a').prop('hash');
$('html, body').animate({
scrollTop: $(target).offset().top
}, 500);
});
//scrollspy
$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
})
//PrettyPhoto
$("a.preview").prettyPhoto({
social_tools: false
});
//Isotope
$(window).load(function(){
$portfolio = $('.portfolio-items');
$portfolio.isotope({
itemSelector : 'li',
layoutMode : 'fitRows'
});
$portfolio_selectors = $('.portfolio-filter >li>a');
$portfolio_selectors.on('click', function(){
$portfolio_selectors.removeClass('active');
$(this).addClass('active');
var selector = $(this).attr('data-filter');
$portfolio.isotope({ filter: selector });
return false;
});
});
});
This is the sendemail.php code
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = $_POST['Name'];
$email = $_POST['Email'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$email_from = $email;
$email_to = 'myemail#domain.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
You are missing the name attribute in your html input tags
This is required for the server side variables, for example:
$name = $_POST['Name'];
will get value from
<input type="text" name="Name" class="form-control" required placeholder="Name">
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input name="Name" type="text" class="form-control" required placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input name="Email" type="text" class="form-control" required placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="Message" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-lg" value="Send Message">
</div>
</div>
</div>
</form>
<?php
if(isset($_POST['submit'],$_POST['Name'],$_POST['Email'],$_POST['Subject'],$_POST['Message'])){
$name = $_POST['Name'];
$email = $_POST['Email'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$email_from = $email;
$email_to = 'myemail#domain.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
$response = array();
if($success){
$response['success'] = true;
$response['message'] = 'Email sent!';
}else{
$response['success'] = false;
$response['message'] = 'Email sent!';
}
}else{
$response['success'] = false;
$response['message'] = 'post variables are not set';
}
echo json_encode($response);
//die;
Change this line in main.js:
$.post($(this).attr('action'), function(data) {
to:
$.post($(this).attr('action'), $(this).serialize(), function(data) {
It will now submit the data.