I want to add Google reCaptcha in this form I tried all things but all in vain.. What I need to do to make form successfully submitted.. and also make reCaptcha a required field using Ajax form submission...
HTML:-
<form id="contact-form" class="uk-form">
<fieldset>
<label>Name <em>*</em></label>
<input type="text" name="name" class="textfield" id="name" value="" />
<label>E-mail <em>*</em></label>
<input type="email" name="email" class="textfield" id="email" value="" />
<label>Services <em>*</em></label>
<input type="text" name="subject" class="textfield" id="subject" value="" />
<label>Message <em>*</em></label>
<textarea name="message" id="message" class="textarea" cols="2" rows="6"></textarea>
<div class="clearfix">
<div class="g-recaptcha" data-sitekey="My_Site_Key"></div>
</div>
<label> </label>
<button type="submit" name="submit" class="uk-button uk-button-small idz-button-blue" id="buttonsend">Submit</button>
<span class="loading" style="display: none;">Please wait..</span>
</fieldset>
</form>
JS:-
$('#buttonsend').click( function() {
var name = $('#name').val();
var subject = $('#subject').val();
var email = $('#email').val();
var message = $('#message').val();
$('.loading').fadeIn('fast');
if (name != "" && subject != "" && email != "" && message != "")
{
$.ajax(
{
url: './contact-us.php',
type: 'POST',
data: "name=" + name + "&subject=" + subject + "&email=" + email + "&message=" + message,
success: function(result)
{
$('.loading').fadeOut('fast');
if(result == "email_error") {
$('#email').css({"background":"#FFFCFC","border":"1px solid #ffadad"}).next('.require').text(' !');
} else {
$('#name, #subject, #email, #message').val("","Name","Subject","Email","Message");
$('<div class="uk-alert uk-alert-success uk-text-center uk-margin-medium-bottom" data-uk-alert><p>Your message has been sent successfully. Thank you!</p></div>').insertBefore('#contact-form');
$('.uk-alert').fadeOut(5000, function(){ $(this).remove(); });
}
}
}
);
return false;
}
else
{
$('.loading').fadeOut('fast');
if( name == "","Name") $('#name').css({"background":"#FFFCFC","border":"1px solid #ffadad"});
if(subject == "","Subject") $('#subject').css({"background":"#FFFCFC","border":"1px solid #ffadad"});
if(email == "","Email" ) $('#email').css({"background":"#FFFCFC","border":"1px solid #ffadad"});
if(message == "","Message") $('#message').css({"background":"#FFFCFC","border":"1px solid #ffadad"});
return false;
}
});
$('#name, #subject, #email,#message').focus(function(){
$(this).css({"border":"none","background":"#fafafa","border":"1px solid #ccc"});
});
PHP:-
$to = "xyz#mydomain.com";
$subject = "Contact Inquiry";
$name = $_POST['name'];
$email = $_POST['email'];
$services = $_POST['subject'];
$message = $_POST['message'];
$str = "Name: ".$name."\r\n\r\n"."Services: ".$services."\r\n\r\n"."Message: ".$message."\r\n\r\n".
$headers = "From: $email";
$sent = mail($to, $subject, $str, $headers) ;
Related
I am trying to set up my page so that when I a form is submitted it emails and a message is displayed without refreshing the page.
I have tried to do this using jQuery/Ajax however I cannot get any emails sent now
Without the Ajax/jQuery, the PHP works just fine but just doesnt include the refresh feature
Any help would be appreciated
PHP (submitForm.php):
<?php
$name = isset($_POST['name']);
$email = isset($_POST['email']);
$phone = isset($_POST['phone']);
$message = isset($_POST['message']);
$feedback = '';
if($name && $email && $phone && $message) {
$name = ($_POST['name']);
$email = ($_POST['email']);
$phone = ($_POST['phone']);
$message = ($_POST['message']);
}
$to = "arshdsoni#gmail.com";
$subject = 'Soni Repairs - Support Request';
$body = <<<EMAIL
Hi There!
My name is $name.
Message: $message.
My email is: $email
Phone Number: $phone
Kind Regards
EMAIL;
$header = "From: $email";
if($_POST) {
if($name == '' || $email == '' || $phone == '' || $message == '') {
$feedback = "Nothing received!";
}
else {
mail($to, $subject, $body, $header);
$feedback = '*Message Received! You will receive a reply shortly!';
}
}
?>
jQuery/Ajax:
function submitForm() {
var name=document.getElementById('name').value;
var dataString = 'name'+ name;
$.ajax({
type:"post",
url:"submitForm.php",
cache:false,
success: function(html) {
$('#feedback').html(html);
}
});
return false;
}
FORM:
<form id="contact" action="#">
<h3>Get in Touch:</h3>
<h4><span id="star" class="glyphicon glyphicon-asterisk"></span>We aim to reply within 24 hours!</h4>
<fieldset>
<input name="name" placeholder="Your Name" type="text" tabindex="1" required>
</fieldset>
<fieldset>
<input name="email" placeholder="Your Email Address" type="email" tabindex="2" required>
</fieldset>
<fieldset>
<input name="phone" placeholder="Your Phone Number" type="tel" tabindex="3" required>
</fieldset>
<fieldset>
<textarea id="textarea" name="message" placeholder="Describe your problem...." tabindex="5" required></textarea>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit submitBtn" data-submit="...Sending" "return submitForm();">Submit</button>
</fieldset>
</form>
Issues:
if you usedocument.getElementById you must use id on your elements
Add the data to your ajax request
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$( "#submitBtn" ).click(function( event ) {
alert('pressed');
//values
var name=document.getElementById('name').value;
var email=document.getElementById('email').value;
var phone=document.getElementById('phone').value;
var message=document.getElementById('message').value;
var dataString = {"name": name, "email":email, "phone": phone, "message":message}
$.ajax({
type:"post",
url:"submitForm.php",
data: dataString,
success: function(html) {
$('#feedback').html(html);
}
});
event.preventDefault();
});
});
</script>
</head>
<body>
<form id="contact" method="POST">
<h3>Get in Touch:</h3>
<h4><span id="star" class="glyphicon glyphicon-asterisk"></span>We aim to reply within 24 hours!</h4>
<fieldset>
<input id="name" placeholder="Your Name" type="text" tabindex="1" required>
</fieldset>
<fieldset>
<input id="email" placeholder="Your Email Address" type="email" tabindex="2" required>
</fieldset>
<fieldset>
<input id="phone" placeholder="Your Phone Number" type="tel" tabindex="3" required>
</fieldset>
<fieldset>
<textarea id="message" placeholder="Describe your problem...." tabindex="5" required></textarea>
</fieldset>
<fieldset>
<button name="submit" id="submitBtn">Submit</button>
</fieldset>
</form>
<div id="feedback"></div>
</body>
</html>
PHP
<?php
if(isset($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['message'])){
//Post data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
//mail settings
$to = "arshdsoni#gmail.com";
$subject = 'Soni Repairs - Support Request';
$body = <<<EMAIL
Hi There!
My name is $name.
Message: $message.
My email is: $email
Phone Number: $phone
Kind Regards
EMAIL;
if(mail($to, $subject, $body, $header)){
$feedback = '*Message sent! You will receive a reply shortly!';
}else{
$feedback = '*Message failed to send';
}
}else{
$feedback = 'Missing Params';
}
echo $feedback;
This is your ajax, where you went wrong
$.ajax({
type:"post",
url:"submitForm.php",
cache:false,
success: function(html) {
$('#feedback').html(html);
}
});
You forgot to include the data to be posted
$.ajax({
type:"post",
url:"submitForm.php",
cache:false,
data {
name: $('#name').val(),
email: $('#email').val(),
phone: $('#phone').val(),
message: $('#msg').val()
}
success: function(html) {
$('#feedback').html(html);
}
});
The keys name, email, message and phone should be as is; because you used $_POST['name'], $_POST['email'], $_POST['message'] and $_POST['phone'].
The calls$('#name').val(), $('#email').val(), $('#phone').val(), $('#message').val() have been made assuming that your input elements have ids name, email, message and phone respectively. JS uses ids rather than names.
Refer to jQuery Ajax documentation for more details.
I do not see you sending the data to PHP file anywhere in your AJAX code
NOTE: Use serialize() function if you want to send all the form data. Else send it separately like
var formDatas = {name:"name",email:"emailID"};
function submitForm() {
var name=document.getElementById('name').value;
var dataString = 'name'+ name;
var formDatas = $('#formID').serialize(); // Send form data
$.ajax({
type:"post",
url:"submitForm.php",
data : {datas : formDatas }
cache:false,
success: function(html) {
$('#feedback').html(html);
}
});
return false;
}
Your references
jQuery Serialize()
jQuery ajax()
I am having trouble getting this to work I want the email to send to me as:
Persons Name <personsName#example.com>
I have been looking at other examples and I think I have everything right I can't find anything out of place in my code. The FROM in the email sent to me is FROM username#just122.justhost.com it is not showing up as either Persons Name or personsName#example.com unless I change the $header to
$header = 'From:' $email;
or
$header = 'From: $name;
Can anyone spot what I am doing wrong?
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$subject = "Message from Blog";
$header = 'From: ' . $name . '<'.$email.'>' . "\r\n";
mail("test#example.com", $subject, $message, $header);
?>
form
<label for="name">Name:</label><input class="inputField" type="text" size="30" id="name"/><div style="display: none;" id="error-name"><font color="#FF0000">Your name here!</font></div><br />
<label for="email">Email:</label><input class="inputField" type="text" size="30" id="email" /> <div style="display: none;" id="error-email"><font color="#FF0000">Your email here!</font></div><br />
<div style="display: none;" id="error-message"><font color="#FF0000">Your message here!</font></div>
<label for="message">Message:</label><textarea id="message" rows="9" cols="59"></textarea> <br />
<input type="button" id="submit-button" value="Send Message" onclick="sendEmail();"/>
<div id="messages">
<div style="display: none;" id="loading"> <font color="#FF0000">Sending..</font></div>
<div style="display: none;" id="message-sent"><font color="#008040">Message sent!</font></div>
<div style="display: none;" id="error-content"><font color="#FF0000">Please enter some content!</font></div>
</div>
<br />
</div>
Ajax
function sendEmail() {
$("#loading").fadeIn(100).show();
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var data = "name=" + name + "&email" + email + "&message=" + message;
if(name == "") {
$("#error-name").fadeIn(700).show();
$("#loading").fadeOut(100).hide();
} else if(email == "") {
$("#error-email").fadeIn(700).show();
$("#loading").fadeOut(100).hide();
} else if(message == "") {
$("#error-message").fadeIn(700).show();
$("#loading").fadeOut(100).hide();
} else {
$.ajax({
type: "POST",
url: "send.php",
data: data,
success: function() {
$("#loading").fadeOut(100).hide();
$('#message-sent').fadeIn(500).show();
}
});
}
}
You had a small typo in the Javascript:
var data = "name=" + name + "&email=" + email + "&message=" + message;
Notice the = at the end of &email=
However, I do want to mention that most hosting providers don't allow you to send emails from domains that aren't on your server. Fortunately this problem turned out to be a typo.
I am trying to setup a fairly straight forward email form that will show a div if the email is successfully submitted and error messages when a field is not properly filled out. Currently, the Jquery side of things work, but it will not send the email. I am new to PHP, so I am not sure how to troubleshoot the PHP side of things, but all the code looks like it is correct.
HTML:
<form id="contactForm" >
<fieldset class="half float-left left">
<div class="overlay-field">
<label for="firstname" class="float-left">First Name</label>
<input id="firstname" type="text" name="firstname" >
<span id="errorFirstName" class="formError"></span>
</div>
<div class="overlay-field" data-error="">
<label for="telephone" class="float-left">Telephone</label>
<input id="telephone" type="text" name="telephone" >
<span id="errorPhone" class="formError"></span>
</div>
</fieldset>
<fieldset class="half float-right right">
<div class="overlay-field">
<label for="lastname" class="float-left">Last Name</label>
<input id="lastname" type="text" name="lastname" >
<span id="errorLastName" class="formError"></span>
</div>
div class="overlay-field" data-error="">
<label for="email" class="float-left">Email Address</label>
<input id="email" type="text" name="email" >
<span id="errorEmail" class="formError"></span>
</div>
</fieldset>
<label for="message" class="textarea-label float-left">Your Message</label>
<textarea id="message" value="Scope of Work" class="description" name="message" ></textarea>
<span id="errorMessage" class="formError"></span>
<div class="message_area">
<p class="success_message">WOOT WOOT! You're message has been sent! We will get back to you shortly.</p>
<p class="error_message">Something went wrong. Please make sure all fields are filled in properly.</p>
</div>
<button type="submit" id="submit" class="bttn bttn-4 bttn-4a">Send Message</button>
</form>
JQUERY:
$(document).ready(function() {
$('#contactForm #submit').click(function() {
$('#contactForm .formError').html('');
var isFocus=0;
var isError=0;
var firstname=$('#contactForm #firstname').val();
var lastname=$('#contactForm #lastname').val();
var email=$('#contactForm #email').val();
var telephone=$('#contactForm #telephone').val();
var message=$('#contactForm #message').val();
if(firstname=='') {
$('#contactForm #errorFirstName').html('This is a required field.');
$('#contactForm #firstname').focus();
isFocus=1;
isError=1;
}
if(lastname=='') {
$('#contactForm #errorLastName').html('This is a required field.');
$('#contactForm #lastname').focus();
isFocus=1;
isError=1;
}
if(telephone=='') {
$('#contactForm #errorPhone').html('This is a required field.');
$('#contactForm #telephone').focus();
isFocus=1;
isError=1;
}
if(email=='') {
$('#contactForm #errorEmail').html('This is a required field.');
if(isFocus==0) {
$('#contactForm #email').focus();
isFocus=1;
}
isError=1;
} else {
var reg = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(email)==false) {
$('#contactForm #errorEmail').html('Invalid email address.');
if(isFocus==0) {
$('#contactForm #email').focus();
isFocus=1;
}
isError=1;
}
}
if(message=='') {
$('#contactForm #errorMessage').html('This is a required field.');
$('#contactForm #message').focus();
isFocus=1;
isError=1;
}
// Terminate the script if an error is found
if(isError==1) {
return false;
}
$.ajaxSetup ({
cache: false
});
$.ajax({
type: "POST",
url: "/wp-content/themes/apds/contact-ajax.php",
data: $("#contactForm").serializeArray(),
success: function(msg) {
$(".success_message").fadeIn().delay(5000).fadeOut();
$("#contactForm")[0].reset();
$('#contactForm #submit').removeAttr("disabled");
},
error: function(ob,errStr) {
$('#contactForm #submit').removeAttr("disabled");
}
});
return false;
});
});
PHP
<?php
if(empty($_POST['firstname']) ||
empty($_POST['lastname']) ||
empty($_POST['telephone']) ||
empty($_POST['email']) ||
empty($_POST['message'])) {
die('Error: Missing variables');
}
$email_subject = "Your email subject line";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
$message=$_POST['message'];
$to='my#email.com';
$headers = 'From: '.$email."\r\n" .
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
$email_subject = $email_subject;
$body='You have got a new message from the contact form on your website.'."\n\n";
$body.='First Name: '.$firstname."\n";
$body.='Last Name: '.$lastname."\n";
$body.='Email: '.$email."\n";
$body.='Telephone: '.$telephone."\n";
$body.='Message: '."\n".$message."\n";
if(mail($to, $email_subject, $body, $headers)) {
die('Mail sent');
} else {
die('Error: Mail failed');
}
?>
I am working on a website with an Ajax contact form. Tried a lot, it successfully sends a mail without headers below is my code please help me to fix this
code
<div class="form">
<div class="title">
<h2 class="orange"><span class="orange">Contact</span> US</h2>
</div>
<div class="height15"></div>
<div id="return_message"></div>
<div class="field">
<label>First Name:</label>
<input name="name" type="text" />
</div>
<div class="field">
<label>Phone Number:</label>
<input name="phone" type="text" />
</div>
<div class="field">
<label>Email Address:</label>
<input name="email" type="text" />
</div>
<label>Message:</label>
<textarea name="message" cols="" rows=""></textarea>
<div class="clear"></div>
<a class="org_btn more submit" id="submit" href="#.">Submit</a> </div>
<script language="javascript">
$(document).ready(function() {
$("#menu_btn").click(function(){
$("#sub_menu").slideToggle("slow");
});
//Contact us form validation
$('#return_message').hide();
$('#submit').click(function(event){
var name = $('#name').val();
var phone = $('#phone').val();
var email = $('#email').val();
var message = $('#message').val();
if( (name=='') || (phone=='') || (email=='') || (message=='') )
{
$('#name').addClass('error_active');
$('#phone').addClass('error_active');
$('#email').addClass('error_active');
$('#message').addClass('error_active');
}
else
{
var regex = /^([a-zA-Z0-9_\.\-\+])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(!regex.test(email))
{
alert("Please Enter valid email address");
$('#email').addClass('error_active');
}
else
{
$.ajax(
{
type: 'POST',
data: ({name : name, phone : phone, email : email, message : message}),
url: 'send_mail.php',
success: function(data)
{
if(data)
{
$('#return_message').show('slow').html("<p>Email has been sent...</p>");
$('#name').val('');
$('#phone').val('');
$('#email').val('');
$('#message').val('');
$('#name').removeClass('error_active');
$('#phone').removeClass('error_active');
$('#email').removeClass('error_active');
$('#message').removeClass('error_active');
}
else
{
$('#return_message').show('slow').html("<p>Email has not been sent...</p>");
}
}
});
}
}
});
});
</script>
php code
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$to = "info#kodspider.com"; // Please put your email addres.
$subject = "Marthoman Vidyapeedom"; //Please put subject of your email.
if($phone!='')
{
$message2 = $message.'\r\nPhone:'.$phone;
}
else
{
$message2 = $message;
}
$message = $message.'\r\nPhone:'.$phone;
$headers = "From: ".$email;
$sent = mail( $to, $subject, $message2, $headers );
if($sent)
{
echo "success";
}
else
{
echo "error";
}
?>
you have given
<input name="name" type="text" />
but in jquery your calling
$('#name').val('')
in jquery # selector is used only for id
make change
<input name="name" id="name" type="text" />
for more details in jquery read this
Okay so I have a form that is using PHP to mail to an e-mail address. I also have a jQuery/AJAX section that validates the form without reloading the page and executes the PHP. For some reason the Product Interest value from the interest drop down is not coming through in the e-mail. What am I missing? I've been driving myself mad with this. Thanks!
This is the form:
<div class="contact-form">
<div class="row">
<div class="field"><input type="text" id="first" name="first" class="input" placeholder="First Name"></div>
<div class="field"><input type="text" id="last" name="last" class="input" placeholder="Last Name"></div>
<div class="field"><input type="text" id="telephone" name="telephone" class="input" placeholder="Phone Number"></div>
</div>
<div class="row">
<div class="field"><input type="email" id="email" name="email" class="input" placeholder="E-mail"></div>
<div class="field">
<select class="select" name="interest" id="interest">
<optgroup label="Countwise Products:">
<option value="I-Count">I-Count</option>
<option value="Q-Count">Q-Count</option>
<option value="D-Count">D-Count</option>
<option value="Z-Count">Z-Count</option>
</optgroup>
</select>
</div>
<input type="submit" value="Get Better Results!" class="button3 dark-blue submit"/>
<span class="error" style="display:none">All Fields Are Required!</span>
<span class="success" style="display:none">Contact Form Submitted Successfully</span>
</div>
</div>
</form>
This is the PHP for titled header_form_email.php
<?php
// POST Variables
//Name
$first = $_POST['first'];
$last = $_POST['last'];
//Phone
$telephone = $_POST['telephone'];
//E-mail
$email = $_POST['email'];
//Interest
$interest= $_POST['interest'];
// Contact subject
$subject = "CountWise Website - Sales Request";
$name = "$first" . " " . "$last";
$message = "You have received a new information request from Countwise.com\n".
"Name: " . "$name\n".
"Phone: " . "$telephone\n".
"Email: " . "$email\n".
"Product Interest: " . "$interest";
// From
$mailheader = "From: $email \r\n";
// Enter your email address
$to ="kelly#drastikdesigns.com";
$send_contact=mail($to,$subject,$message,$mailheader);
// Check, if message sent to your email
if($send_contact){
echo "<strong>We have received your information and will be in contact you shortly. Thank you.</strong>" ;
}
else {
echo "ERROR";
}
?>
This is the AJAX
$(function() {
$(".submit").click(function() {
var first = $("#first").val();
var last = $("#last").val();
var telephone = $("#telephone").val();
var email = $("#email").val();
var interest = $("#interest").val();
var dataString = 'first=' + first + '&last=' + last + '&telephone=' + telephone + '&email=' + email + '&interest' + interest;
if (first == '' || last == '' || telephone == '' || email == '') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else {
$.ajax({
type: "POST",
url: "http://www.drastikdesigns.com/clients/countwise/php/header_form_email.php",
data: dataString,
success: function() {
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
You forgot the equals:
'&interest=' + interest;
On a side note, why not just do:
data:$("#formid").serialize(),
That will save you tons of time and headache. Second point, using firebug/developers tools/chrome definitely helps when it comes to viewing the headers sent via ajax.