I have a contact form that is working fine, it sends the email except that it doesn't post the email address of the person that sends it. I have no idea why, I tried changing the $from variable and nothing changes. Is there something obvious I am missing?
It is the input with the id c_email which is put into the $from variable that I do not receive in the emails sent by this form.
My contactform.php:
<?php
// Contact
$to = 'myemail#gmail.com';
$subject = 'Portfolio ContactForm';
if(isset($_POST['c_name']) && isset($_POST['c_email']) && isset($_POST['c_message'])){
$name = $_POST['c_name'];
$from = $_POST['c_email'];
$message = $_POST['c_message'];
if (mail($to, $subject, $from, $name, $message)) {
$result = array(
'message' => 'Sent, thanks!',
'sendstatus' => 1
);
echo json_encode($result);
} else {
$result = array(
'message' => 'Ooops, problem..',
'sendstatus' => 1
);
echo json_encode($result);
}
}?>
On my html page:
<form id="contact-form" role="form">
<div class="form-group">
<label class="sr-only" for="c_name">Name</label>
<input type="text" id="c_name" class="form-control" name="c_name" placeholder="Nom">
</div>
<div class="form-group">
<label class="sr-only" for="c_email">Email address</label>
<input type="email" id="c_email" class="form-control" name="c_email" placeholder="E-mail">
</div>
<div class="form-group">
<textarea class="form-control" id="c_message" name="c_message" rows="7" placeholder="Votre message"></textarea>
</div>
<button type="submit" class="btn btn-custom-1">
<i class="fa fa-bullhorn icon-before"></i> Envoyer
</button>
</form>
Your values is not posting because in your form you not mention method="POST".Just try like this.it should work
<form id="contact-form" role="form" action="" method="POST">
<div class="form-group">
<label class="sr-only" for="c_name">Name</label>
<input type="text" id="c_name" class="form-control" name="c_name" placeholder="Nom">
</div>
<div class="form-group">
<label class="sr-only" for="c_email">Email address</label>
<input type="email" id="c_email" class="form-control" name="c_email" placeholder="E-mail">
</div>
<div class="form-group">
<textarea class="form-control" id="c_message" name="c_message" rows="7" placeholder="Votre message"></textarea>
</div>
<button type="submit" class="btn btn-custom-1">
<i class="fa fa-bullhorn icon-before"></i> Envoyer
</button>
</form>
<script>
$('button').click(function() {
var c_name = $("#c_name").val();
var c_email = $("#c_email").val();
var c_message = $("#c_message").val();
$.ajax({//create an ajax request to load_page.php
type: "POST",
url: "assets/php/contactForm.php",
data:{"c_name":c_name,"c_email":c_email,"c_message":c_message},
success: function(data) {
if (data) {
alert(data);
}
else {
alert('Successfully not posted.');
}
}
});
});
</script>
Related
I have create form and i am trying to store the data using Ajax.but data is not inserted to the database.what can be the error.user_id will be auto increment. and also i want to validate sitename.it should restrict inserting same sitenames.
Here is my code
Controller
public function user_add()
{
$data_save = array(
"Mnumber" => $this->input->post("Mnumber"),
"email" => $this->input->post("email"),
"fname" => $this->input->post("fname"),
"address" => $this->input->post("address"),
"sitename" => $this->input->post("sitename"),
"title" => $this->input->post("title"),
"descr" => $this->input->post("descr")
);
if ($this->user_mod->AddUser($data_save)) {
echo "Successfully Saved";
} else {
echo "error";
}
}
}
Model
class user_mod extends CI_Model
{
public function AddUser($data_save)
{
if ($this->db->insert('users', $data_save)) {
return true;
} else {
return false;
}
}
}
View
<script>
function save_user_new() {
var user_id = $('#user_id').val();
var Mnumber = $('#Mnumber').val();
var email = $('#email').val();
var fname = $('#fname').val();
var address = $('#address').val();
var sitename = $('#sitename').val();
var title = $('#title').val();
var descr = $('#descr').val();
if (sitename != "" && Mnumber != "") {
$.ajax({
type: "post",
async: false,
url: "<?php echo site_url('form_con/user_add'); ?>",
data: { "user_id": user_id,
"Mnumber": Mnumber,
"email": email,
"fname": fname,
"address": address,
"sitename": sitename,
"title": title,
"descr": descr,
},
dataType: "html",
success: function (data) {
if (data == 'error') {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : Something wrong.");
} else if (data == 'have') {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : This Employee is already exists.");
} else {
$('#error_msg1').hide();
$('#success_msg').show();
$('#success_msg').html("Entitled leave successfully saved.");
$('#load_leave').html(data);
}
}
});
} else {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : Please enter Employee Details.");
}
}
</script>
<form action="#">
<div class="form-body">
<div class="form-group">
<label class="control-label">Your First Name</label>
<input type="text" class="form-control" id="fname" name="fname" placeholder="Enter text">
<!--<span class="help-block"> A block of help text. </span>-->
</div>
<div class="form-group">
<label class="control-label">Email Address</label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-envelope"></i>
</span>
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="form-group">
<label class="control-label">Your Mobile Number</label>
<input type="text" class="form-control" id="Mnumber" name="Mnumber" placeholder="Enter text">
<!--<span class="help-block"> A block of help text. </span>-->
</div>
<div class="form-group">
<label class="control-label">Your Address</label>
<input type="text" class="form-control" id="address" name="address" placeholder="Enter text">
</div>
<div class="form-group">
<label class="control-label">Your Site Name</label>
<input type="text" class="form-control" id="sitename" name="sitename" placeholder="Enter text">
<!--<span class="help-block"> A block of help text. </span>-->
</div>
<div class="form-group">
<label class="control-label">Title of Your Web site</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Enter text">
</div>
<div class="form-group">
<label class="control-label">Description of Your Web Site</label>
<input type="text" class="form-control" id="descr" name="descr" placeholder="Enter text">
</div>
</div>
<div class="form-actions right">
<button type="submit" class="btn green" onclick="save_user_new()">Submit</button>
<button type="button" class="btn default">Cancel</button>
</div>
</form>
<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.
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'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;
});
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);
}