I've been looking to solve this, but i can't find a way to do it. I already tried other methods i found in here, but none of them were working for me.
I'm building a website with Bootstrap and also using Bootstrap Validator to make a Contact Form. I can make it work perfect and also submit the contact, but the "thank you message" is redirected to another page. I want the "Thank you message" to load on the same page, and take the place of the form, since its a one scrolling page website.
Can anyone help me please? This is the code i have so far:
Ps.: All my CSS file are the original from bootstrap, nothing has been changed.
HTML
<div class="container">
<div class="row">
<!-- form: -->
<section>
<div class="col-lg-8 col-lg-offset-2">
<div class="page-header">
<h2>Formulário de contato.</h2>
</div>
<form id="defaultForm" method="post" class="form-horizontal" action="contact-2.php">
<div class="form-group">
<label class="col-lg-3 control-label">Nome completo</label>
<div class="col-lg-4">
<input type="text" class="form-control" name="firstName" placeholder="Nome" />
</div>
<div class="col-lg-4">
<input type="text" class="form-control" name="lastName" placeholder="Sobrenome" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Assunto</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="subject" placeholder="WebSite" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">E-mail</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="email" placeholder="email#contato.com" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Telefone</label>
<div class="col-lg-5">
<input type="tel" class="form-control" name="tel" placeholder="48 0000 0000" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Mensagem</label>
<div class="col-lg-5">
<textarea class="form-control" name="message" rows="10" placeholder="Escreva aqui sua mensagem."></textarea>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label" id="captchaOperation"></label>
<div class="col-lg-2">
<input type="text" class="form-control" name="captcha" />
</div>
</div>
<div class="form-group">
<div class="col-lg-9 col-lg-offset-3">
<button type="submit" class="btn btn-primary" name="signup" value="Sign up">Enviar</button>
<button type="button" class="btn btn-info" id="resetBtn">Limpar</button>
</div>
</div>
</form>
</div>
</section>
<!-- :form -->
</div>
</div>
PHP Contact
<?php
// getting
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = trim($_POST['email']);
$emailtosend = 'myemail#hotmail.com'; //E-mail to receive message
$tel = $_POST['tel'];
$subject = $_POST['subject'];
$message = $_POST['message'];
/* Message to show on email. */
$mensagemHTML = '<P>Contact form sent by the website.</P>
<p><b>First Name:</b> '.$firstname.'
<p><b>Last Name:</b> '.$lastname.'
<p><b>E-Mail:</b> '.$email.'
<p><b>Telephone:</b> '.$tel.'
<p><b>Subject:</b> '.$subject.'
<p><b>Message:</b> '.$message.'</p>
<hr>';
//
//
$headers = "MIME-Version: 1.1\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: $email\r\n"; // remetente
$headers .= "Return-Path: $emailtosend \r\n"; // return-path
$envio = mail($email, $subject, $mensagemHTML, $headers);
if($envio)
//echo "<script>location.href='sucesso.html'</script>"; // Página que será redirecionada
?>
If there is anyone here able to help me with it, that would be great!
Have a look at the code below. You need AJAX. Place this in your html page.
The whole project of that code can be found at this link (just in case you want to see other files too).
$(function() {
$("#myform button").click(function() {
// Get form values
var name = $("input#name").val();
var email = $("input#email").val();
var comments = $("textarea#comments").val();
// Validate and prepare values for php
var dataString = 'name='+ name + '&email=' + email + '&comments=' + comments;
// Post data to the php file
$.ajax({
type: "POST",
url: "bin/process-form.php",
async:false, // Wait for the result
data: dataString,
success: function(data) {
if (data=="Email Sent Successfully!"){
$('#myform').html("<div id='message'></div>");
$('#message').html("<h2>Enquiry Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<span class='glyphicon glyphicon-ok'></span>");
});
} else {
$("#myform-errors").show();
$('#myform-errors').html(data);
}
}
});
return false;
});
Related
<div class="row">
<div class="col-xs-12 col-lg-10 col-lg-offset-1">
<form method="post" action="" name="contactForm" id="contactform" class="clearfix">
<fieldset>
<div class="float-left">
<div class="coolfx fadeInUp">
<!--<span>*Name<label for="name"></label></span>-->
<span><input type="text" id="contactName" name="name" placeholder="*Name" class="text" required></span>
</div>
<div class="coolfx fadeInUp" >
<!--<span>*Email<label for="email"></label></span>-->
<span><input type="email" id="contactEmail" name="email" placeholder="*Email" class="email" required></span>
</div>
<div class="coolfx fadeInUp">
<!--<span>Phone<label for="phone"></label></span>-->
<span><input type="text" id="contactPhone" name="phonenumber" placeholder="Phone" class="text" required></span>
</div>
</div>
<div class="float-right">
<div class="contactform message coolfx fadeInUp">
<!--<span>Message<label for="message"></label></span>-->
<span><textarea id="contactMessage" placeholder="*Message" name="message" class="textarea" required></textarea></span>
</div>
</div>
</fieldset>
<div class="float-right"><div class="g-recaptcha" data-sitekey="6LfsPBgTAAAAAPDkaI1HeSyDm_ecF0iihVsFYBKh"></div></div>
<div class="coolfx fadeInUp">
<input name="send" type="submit" class="submit" id="submit" value="Send Email">
</div>
<?php
if(isset($_POST['g-recaptcha-response']))
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "******************************";// hide for security
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if (isset($data->success) AND $data->success==true) {?>
<script type="text/javascript">
$(function() {
$("#contactform .submit").click(function() {
var data = {
name: $("#contactName").val(),
email: $("#contactEmail").val(),
phone: $("#contactPhone").val(),
message: $("#contactMessage").val()
};
$.ajax({
type: "POST",
url: "email.php",
data: data,
success: function(){
$('.success').fadeIn(1000);
}
});
});
});
</script>
<?php }else {
echo"this is spam"
}?>
I am new in j query how to use j query code inside the php code and also how to verify google captcha code before submit a mail function nothing working .
before google captcha working my code properly but now nothing working.
thanks
You need to handle the Captcha inside email.php. Before getting other posted field values you can check for Capcha response. Also don't forget that you need to post the captcha field (g-recaptcha-response) value like you post name, phone, email and messsage.
Im not receiving emails from my email form.
I created a simple form that takes 4 fields and sends them to our emails. I tested my php code with no errors. I double checked the html to see if the script was referring properly which it was. I believe that the ids and names are correct. I also triple checked the variables I created on the php code. I've googled everything I could come up with no luck. Thanks!
var dump $body shows
Thank you! One of our station representatives will contact you soon.
string(119) "
Name: TEST
Email: test#emaol.com
Station: 1234
Message:
I need help with.
FORM
<form class="form-horizontal" role="form" method="post" action="feedback.php">
<fieldset>
<!-- Form Name -->
<legend>Our team will reach out to you as soon as possible.</legend>
<!-- Text input-->
<div class="control-group col-xs-4">
<label class="control-label" for="textinput">Full Name</label>
<div class="controls">
<input id="name" name="name" placeholder="John Doe" class="input-xlarge" type="text">
</div>
</div>
<!-- Text input-->
<div class="control-group col-xs-4">
<label class="control-label" for="textinput">E-Mail</label>
<div class="controls">
<input id="email" name="email" placeholder="affiliate.marketing.app#123.com" class="input-xlarge" type="text">
</div>
</div>
<!-- Text input-->
<div class="control-group col-xs-4">
<label class="control-label" for="textinput">Station Code</label>
<div class="controls">
<input id="station" name="station" placeholder="xxx" class="input-xlarge" type="text">
</div>
</div>
<!-- Textarea -->
<div class="control-group">
<label class="control-label" for="textarea">Message</label>
<div class="controls">
<textarea id="message" name="message">I need help with....</textarea>
</div>
</div>
<!-- Button -->
<div class="control-group">
<label class="control-label" for="submit"></label>
<div class="controls">
<button type="submit" id="submit" value="Send" name="singlebutton" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
PHP PAGE
<?php
/* These are the variable that tell the subject of the email and where the email will be sent.*/
$emailSubject = 'Station App Support Request';
$mailto = 'myemail#domain.com, myemail2#domain.com';
/* These will gather what the user has typed into the fieled. */
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$stationField = $_POST['station'];
$messageField = $_POST['message'];
/* This takes the information and lines it up the way you want it to be sent in the email. */
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Station: $stationField <br>
Message: $messageField <br>
EOD;
$headers = "From: $emailField\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
?>
I have the following code in my PHP form. How would i adjust it to include my Google Anayltic Code below?:
<?php
$sendMail="";
if (isset($_POST["sendemail"])){
$from = $_POST["email"];
$subject = $_POST["name"];
$message = $_POST["message"];
$message = wordwrap($message, 70);
$m=mail(get_post_meta( get_the_ID(), 'Seller Email', true ),$subject,$message,"From: $from\n");
if($m){
$sendMail="Message sent successfully.";
}else{
$sendMail="Message not sent.";
}
} ?>
<!--=== Contact Form ===-->
<form role="form" class="contactform" method="post">
<div class="form-group">
<label for="email">Your email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" data-original-title="" title="">
</div>
<div class="form-group">
<label for="name">Your name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" data-original-title="" title="">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" placeholder="Information regarding property REF:<?php the_title();?>" style="height:100px;"></textarea>
</div>
<div class="form-group">
<button type="submit" name="sendemail" class="btn btn-lg btn-color">Send</button>
</div>
<?php if($sendMail!='') echo '<div class="form-group">'. $sendMail .'</div>';?>
</form>
</div><div style="clear:both;"></div>
My Google Analytics is the following, and I am unsure how I could add this onto the form on submission:
<script>
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-50671476-1', 'auto');
ga('send', 'pageview');
</script>
You can track form submission by creating the Goal in your Google Analytics Account.
Goal Type : Destination Url
DO one thing, On Successfully Submitted the form take the user to another page and show "Message Successfully Sent" there and set the Goal on that page so that you can track the users who successfully submitted the form.
Code :
<?php
$sendMail="";
if (isset($_POST["sendemail"])){
$from = $_POST["email"];
$subject = $_POST["name"];
$message = $_POST["message"];
$message = wordwrap($message, 70);
$m=mail(get_post_meta( get_the_ID(), 'Seller Email', true ),$subject,$message,"From: $from\n");
if($m){
header('location:successfull.html');
}else{
$sendMail="Message not sent.";
}
} ?>
<!--=== Contact Form ===-->
<form role="form" class="contactform" method="post">
<div class="form-group">
<label for="email">Your email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" data-original-title="" title="">
</div>
<div class="form-group">
<label for="name">Your name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" data-original-title="" title="">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" placeholder="Information regarding property REF:<?php the_title();?>" style="height:100px;"></textarea>
</div>
<div class="form-group">
<button type="submit" name="sendemail" class="btn btn-lg btn-color">Send</button>
</div>
<?php if($sendMail!='') echo '<div class="form-group">'. $sendMail .'</div>';?>
</form>
</div><div style="clear:both;"></div>
successfull.html :
Message Successfully Sent
Steps you have to done in Google Analytics account to setup the Goal :
Admin -> View -> Goals -> New Goal -> Goal Setup -> Custom
I am trying to send an email with PHP and Ajax, the mailer is working but I know very little about Ajax, after reading a couple of pages of a book I have written a small piece of code and was hoping someone could point out where I am going wrong:
Here is my PHP:
<?php
$name = ($_POST['name']);
$email = ($_POST['email']);
$fsubject = ($_POST['subject']);
$message = ("Name: ". $name . "\nEmail Address: " . $email . "\n\nMessage: " . $_POST['message']);
// Set Mail
$to = "emailaddress#fakeone.com";
$headers = 'emailaddress#fakeone.com' . "\r\n";
$subject = "{$fsubject}";
$body = "{$message}";
// Send Mail
if (mail($to, $subject, $body, $header))
{
echo("<p>Message successfully sent!</p>");
}
else
{
echo("<p>Message delivery failed...</p>");
}
?>
My HTML:
<div id="success" style="color:red;"></div>
<form action="" id="contactform" method="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control" name="name" id="name">
</div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control" name="subject" id="subject">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message" id="message"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" id="submit" value="Send Message" class="btn btn-info btn-lg" data-loading-text="Loading...">
</div>
</form>
My AJAX:
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.post("sendmail.php", $("#contactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
</script>
At the moment the same page just reloads, the desired outcome is just a subtle success or failure message appearing.
Any help would be welcome!
Thanks,
Dan
Your snippet code seems to run just fine.
It must be an error either in other JS (check browser console, F12 in Chrome for example) or in your PHP.
Try this to see if ajax post fails:
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.post("contact.php", $("#contactform").serialize(), function(response) {
$('#success').html(response);
}).fail(function() {
alert( "error" );
});
return false;
});
});
</script>
Also, try to make sure PHP execution is fine, so remove all javascript and replace
<form action="sendmail.php" id="contactform" method="post">
Check the result of that page, it should be "Message successfully sent!".
Also, check a simple JS snippet of your code:
http://jsfiddle.net/CPtpk/
Please note that if you have another JavaScript code in your page that could be a cause for this problem. Even if your JS error is in another place it can cause JS execution to stop and so your onClick isn't executed and that's why you get your problem...
Try something like this on the JS:
<script>
$(document).ready(function() {
$('#contactform').submit(function(e) {
e.preventDefault();
$.post("sendmail.php", $(this).serialize(), function(response) {
$('#success').html(response);
});
});
});
</script>
Cant seem to see why this contact form is not working any pointers would be great... wanna send the form via ajax and serialise the data to be sent across.. just doesnt seem to be working though..hope some one can help
html
<form method="post" action="" id="contact_form">
<div class="row">
<div class="large-6 columns">
<label for="contact_name">Your Name</label>
<input type="text" placeholder="" id="contact_name" name="contact_name" class="required">
</div>
<div class="large-6 columns">
<label for="contact_email">Your Email</label>
<input type="text" placeholder="" id="contact_email" name="contact_email" class="required">
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label for="contact_company">Company Name or Organization</label>
<input type="text" placeholder="" id="contact_company" name="contact_company" class="required">
</div>
<div class="large-6 columns">
<label for="contact_phone">Phone Number</label>
<input type="text" placeholder="" id="contact_phone" name="contact_phone" class="required">
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label for="contact_message">Your Message</label>
<textarea rows="4" placeholder="" id="contact_message" name="contact_message" class="required"></textarea>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<input type="submit" name="contact_submit" id="contact_submit" value="SEND MESSAGE" class="button">
</div>
</div>
</form>
<p class="success" style="display:none">Your message has been
sent successfully.</p>
Mail.php
<?php
$name = $POST['contact_name'] ;
$email = $POST['contact_email'] ;
$company = $_POST['contact_company'] ;
$number = $_POST['contact_phone'] ;
$message = $POST['contact_message'] ;
mail("allycallow#hotmail.com", $name, $company, $number, $message, "From:" . $email);
?>
js file
$(document).ready(function() {
$('#contact_form').validate({
submitHandler: function(form) {
//do submit
var dataString = $("this").serialize();
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "php/mail.php",
data: dataString,
success: function() {
$('.success').fadeIn(1000);
$("input[type=text], textarea").val("");
$('.success').fadeToggle(1000);
}
});
return false;
}
});
});
You have made some mistakes. Use the below code:
<?php
$name = $_POST['contact_name'] ;
$email = $_POST['contact_email'] ;
$company = $_POST['contact_company'] ;
$number = $_POST['contact_phone'] ;
$message = $_POST['contact_message'] ;
//modify the mail function
mail("allycallow#hotmail.com", $name.$company, $message, "From:" . $email);
?>
And in JS change the below:
success: function(returnData) {
$('.success').fadeIn(1000);
$("input[type=text], textarea").val("");
//$('.success').fadeToggle(1000);
}