jQuery validation with codeigniter and ajax submission not working - php

I am use codeigniter framework to create a registration form I have also use jQuery to validate the fields the validation is working but after filling all details and click register button nothing is happen jQuery does not post data to controller. please help
Here is my html code:
<form method="post" action="" name="sentMessage" id="contactForm" novalidate >
<div class="row">
<div class="col-sm-12">
<h3>Registration Form</h3>
</div>
<div class="col-sm-12">
<div class="form-group">
<input class="text required" type="text" placeholder="UserName" name="username" id="uname" data-validation-required-message="Please enter your UserName." />
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input class="text required" type="text" placeholder="First Name" name="fname" id="fname" data-validation-required-message="Please enter your First Name."/>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input class="text required " type="text" placeholder="Last Name" name="lname" id="lname" data-validation-required-message="Please enter your Last Name."/>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input class="text required email" type="email" placeholder="Email" name="email" id="uemail" data-validation-required-message="Please enter your Email."/>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input class="text required" type="password" placeholder="Password" name="password" id="upassword" data-validation-required-message="Please enter your Password."/>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input class="text required" type="password" placeholder="Confirm Password" name="cpassword" id="cpassword" data-validation-required-message="Please enter your Password."/>
<p class="help-block text-danger"></p>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 rst-contactsubmit">
<div class="form-group">
<!-- required data-validation-required-message="Please enter your message." -->
<p class="help-block text-danger"></p>
<div id="success"></div>
</div>
<input class="btn btn-primary btn-lg" type="submit" value="REGISTER" />
</div>
</div>
</form>
My jquery code :
if( jQuery("#contactForm").length ) {
jQuery("#contactForm input").jqBootstrapValidation({
preventSubmit: true,
submitError: function(jQueryform, event, errors) {
// additional error messages or events
},
submitSuccess: function(jQueryform, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var username = jQuery("input#contact-username").val();
var fname = jQuery("input#contact-fname").val();
var lname = jQuery("input#contact-lname").val();
var email = jQuery("input#contact-email").val();
var password = jQuery("input#contact-password").val();
var firstName = fname; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = fname.split(' ').slice(0, -1).join(' ');
}
jQuery.ajax({
type: "POST",
url: "<?php echo base_url()?>index.php/login/create_user",
data: {
username: username,
fname: fname,
lname: lname,
email: email,
password: password,
},
cache: false,
success: function() {
// Success message
jQuery('#success').html("<div class='alert alert-success'>");
jQuery('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
jQuery('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
jQuery('#success > .alert-success')
.append('</div>');
//clear all fields
jQuery('#contactForm').trigger("reset");
},
error: function() {
// Fail message
jQuery('#success').html("<div class='alert alert-danger'>");
jQuery('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
jQuery('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that server is not responding. Please try again later!");
jQuery('#success > .alert-danger').append('</div>');
//clear all fields
jQuery('#contactForm').trigger("reset");
},
})
},
filter: function() {
return jQuery(this).is(":visible");
}
});
}
And my controller code :
<?php
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index(){
$this->load->helper(array('form'));
$this->load->view('login');
}
function validate_credentials(){
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query){
$data = array('username' => $this->input->post('username'), 'is_logged_in' => true);
$this->session->set_userdata($data);
redirect('<?php base_url()index.php/user_dashboard');
}
}
function create_user(){
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_check_username');
$this->form_validation->set_rules('fname', 'Name', 'trim|required');
$this->form_validation->set_rules('lname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email-id', 'trim|required|valid_email|callback_check_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE){
echo "fail";
}
else{
$this->load->model('membership_model');
if($query = $this->membership_model->create_user())
{
$data['account created'] = 'your account is created';
$this->load->view('login', $data);
}
else{
$this->load->view('register');
}
}
}
function check_username($requested_username){
$this->load->model('membership_model');
$username_available = $this->membership_model->check_username($requested_username);
if($username_available){
return TRUE;
}
else{
return FALSE;
}
}
function check_email($requested_email){
$this->load->model('membership_model');
$email_available = $this->membership_model->check_email($requested_email);
if($email_available){
return TRUE;
}
else{
return FLASE;
}
}
}
?>

The problem seems to be in the jQuery code
url: "<?php echo base_url()?>index.php/login/create_user",
jQuery doesn't read PHP code. try changing it to
url: "login/create_user",
Hope this helps.

Codeigniter Validation will not work with Jquery/Ajax Form Submission. Please manually do the validation...
foreach($_POST as $key =>$value){
if($value==''){
echo 'Error';
}
}
By this server response you can alert the user Or else simply
https://jqueryvalidation.org/documentation/ use this client side validation.

Related

Not Getting ajax success response using mamp

For some reason I am not getting a response back from AJAX call. I have used the console in Chrome and Firefox to monitor this but all I can see if what the form is sending. I am using MAMP and haven't tried this on a live server, but I need it to work in MAMP. I have tried lots of things people have suggested from similar issues I have found in stackoverflow but known have worked,please help!
<form id="signup_submit" method="post" action="AJAX/signup.php">
<div class="form-group show-progress">
</div>
<div class="form-group">
<input type="text" name="firstname" id="firstname" class="form-control" autocomplete="firstname" placeholder="Enter First Name...">
<span class="firstname-error error"></span>
</div><!-- form-group -->
<div class="form-group">
<input type="text" name="surname" autocomplete="surname" id="surname" class="form-control" placeholder="Enter Surname...">
<span class="surname-error error"></span>
</div><!-- form-group -->
<div class="form-group">
<input type="email" name="email" id="email" autocomplete="email" class="form-control" placeholder="Enter Email...">
<span class="email-error error"></span>
</div><!-- form-group -->
<div class="form-group">
<input type="password" name="password" autocomplete="password" id="password" class="form-control" placeholder="Choose Password...">
<span class="password-error error"></span>
</div><!-- form-group -->
<div class="form-group">
<input type="password" id="confirm" name="confirm" autocomplete="confirm" class="form-control" placeholder="Confirm Password...">
<span class="confirm-error error"></span>
</div><!-- form-group -->
<div class="form-group">
<button type="submit" id="submit" >Create Account</button>
</div><!-- form-group -->
<div class="form-group">
Already have an account?
</div>
</form><!-- form -->
signup.js
$(document).ready(function(){
$('form').submit(function(e){
e.preventDefault();
var data = $('#signup_submit').serialize();
console.log(data)
$.ajax({
type:'POST',
URL: 'AJAX/signup.php',
data: data,
dataType: 'json',
encode:true,
success: function(d){
d = JSON.parse(d);
console.log(d.testjson);
if(d.success){
$('#signup_submit').append('<div>'+d.message+'</div>');
}else{
if(d.errors.firstname){$('.firstname-error').show();$('.firstname-error').html(d.errors.firstname);}
if(d.errors.surname){$('.surname-error').show();$('.surname-error').html(d.errors.surname);}
if(d.errors.email){$('.email-error').show();$('.email-error').html(d.errors.emailname);}
if(d.errors.password){$('.password-error').show();$('.password-error').html(d.errors.password);}
if(d.errors.confirm){$('.confirm-error').show();$('.confirm-error').html(d.errors.confirm);}
}
}
})
})
})
signup.php
<?php
$errors = array();
$response = array();
if(empty($_POST['firstname'])){$errors['firstname'] = 'First Name Is Required!';}
if(empty($_POST['surname'])){$errors['surname'] = 'Surname Is Required!';}
if(empty($_POST['email'])){$errors['email'] = 'Email Address Is Required!';}
if(empty($_POST['password'])){$errors['password'] = 'Password Is Required!';}
if(empty($_POST['confirm'])){$errors['confirm'] = 'Please Confirm Your Password!';}
$response['errors'] = $errors;
if (!empty($errors)){
$response['success'] = false;
$response['message'] = 'FAIL!';
}else{
$response['success'] = true;
$response['message'] = 'SUCCESS!';
}
?>
You are never returning anything in the signup.php file. You need to json_encode and use echo or exit at the end of your script.
<?php
$errors = array();
$response = array();
if (empty($_POST['firstname'])) $errors['firstname'] = 'First Name Is Required!';
if (empty($_POST['surname'])) $errors['surname'] = 'Surname Is Required!';
if (empty($_POST['email'])) $errors['email'] = 'Email Address Is Required!';
if (empty($_POST['password'])) $errors['password'] = 'Password Is Required!';
if (empty($_POST['confirm'])) $errors['confirm'] = 'Please Confirm Your Password!';
$response['errors'] = $errors;
if (!empty($errors)) {
$response['success'] = false;
$response['message'] = 'FAIL!';
} else {
$response['success'] = true;
$response['message'] = 'SUCCESS!';
}
// Return data here
exit(json_encode($response));
?>
Edit: Keep the PHP exit from above, but instead use jQuery.post() rather than jQuery.ajax() as it is shorter, cleaner and simpler than messing with content types. It should also fix your issue:
$(document).ready(function(){
$('form').submit(function(e) {
e.preventDefault();
var data = $('#signup_submit').serialize();
console.log("data:");
console.log(data);
$.post("AJAX/signup.php", data, result => {
console.log("result:");
console.log(result);
result = JSON.parse(result);
if (result.success) {
$('#signup_submit').append('<div>'+result.message+'</div>');
} else {
if (result.errors.firstname) $('.firstname-error').html(result.errors.firstname).show();
if (result.errors.surname) $('.surname-error').html(result.errors.surname).show();
if (result.errors.email) $('.email-error').html(result.errors.email).show();
if (result.errors.password) $('.password-error').html(result.errors.password).show();
if (result.errors.confirm) $('.confirm-error').html(result.errors.confirm).show();
}
});
});
});
Remove dataType: 'json' and try. Also set the header before echoing back the result like below:
header('Content-type:application/json');
echo json_encode($response);

Email won't send when additional fields are added

I've had a working contact form on a site that was doing just fine. No performance issues whatsoever.
I was asked to add a required Phone and Address field, and it has stopped sending. I cannot find any coding errors, so I am pleading for help here. Sometimes you need a fresh set of eyes to figure out what is wrong.
Note: The jquery validation file used is the standard Bootstrap version, unedited.
Thank you in advance!
The HTML:
<form name="sentMessage" id="contactForm" novalidate>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" id="name" class="form-control" placeholder="Name" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="email" id="email" class="form-control" placeholder="Email" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" id="phone" class="form-control" placeholder="Phone" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" id="address" class="form-control" placeholder="Address" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Message" required></textarea>
<p class="help-block text-danger"></p>
</div>
<div id="success"></div>
<button type="submit" class="btn btn-custom btn-lg">Send Message</button>
</form>
And the PHP behind it:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['address']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'Sample for Stack#Stack.com'; //
$email_subject = "XXX Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:
\n\nName: $name
\n\nEmail: $email_address
\n\nPhone: $phone
\n\nAddress: $address
\n\nMessage:\n$message";
$headers = "From: noreply#example.com\n"; //
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
And the JS:
$(function() {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var address = $("input#address").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "././mail/contact_me.php",
type: "POST",
data: {
name: name,
email: email,
phone: phone,
address: address,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
Here's my complete working solution.
mail.php
<?php
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'test#test.com'; //
$email_subject = "XXX Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:
\n\nName: $name
\n\nEmail: $email_address
\n\nPhone: $phone
\n\nAddress: $address
\n\nMessage:\n$message";
$headers = "From: noreply#example.com\n"; //
$headers .= "Reply-To: $email_address";
if(mail($to,$email_subject,$email_body,$headers)) {
return 'true';
} else {
return 'false';
}
?>
mail.html (includes the validation JS)
<form name="sentMessage" id="contactForm" novalidate>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" id="name" class="form-control" placeholder="Name" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="email" id="email" class="form-control" placeholder="Email" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" id="phone" class="form-control" placeholder="Phone" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" id="address" class="form-control" placeholder="Address" required="required">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Message" required></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div id="success"></div>
<button type="submit" class="btn btn-custom btn-lg">Send Message</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <!-- or use local jquery -->
<script src="js/jqBootstrapValidation.js"></script>
<script>
$(function() {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var address = $("input#address").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "mail.php",
type: "POST",
data: {
name: name,
email: email,
phone: phone,
address: address,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
</script>
And then of course if have the jqBootstrapValidation.js file in a directory of /js/

Issue with PHPMailer, email won't send

I am trying to get my contact form to work. I have uploaded the website on the live server which uses PHP 5.4. I fill out the contact form and it says "Your Message has been sent" but I don't receive any emails for some reason.
This is HTML Form.
<form role="form" id="contactForm">
<div class="form-group control-group">
<div class="controls">
<p class="help-block"></p>
<input type="text" class="form-control"
placeholder="First Name" id="firstname"
data-validation-required-message="Please enter your first name."
aria-invalid="false">
</div>
</div>
<div class="form-group control-group">
<div class="controls">
<p class="help-block"></p>
<input type="text" class="form-control"
placeholder="Last Name" id="lastname"
data-validation-required-message="Please enter your last name."
aria-invalid="false">
</div>
</div>
<div class="form-group control-group">
<div class="controls">
<p class="help-block"></p>
<input type="email" class="form-control"
placeholder="Email" id="email"
data-validation-required-message="Please enter your email address."
aria-invalid="false">
</div>
</div>
<div class="form-group control-group">
<div class="controls">
<p class="help-block"></p>
<textarea class="form-control" placeholder="Message"
id="message" data-validation-required-message="Please enter some message."
aria-invalid="false"></textarea>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Send message</button>
</div>
</form>
This is where it goes wrong potentially. I have the url: "send.php" file in my directory...
<script>
$(function () {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var firstname = $("input#firstname").val();
var lastname = $("input#lastname").val();
var email = $("input#email").val();
var message = $("textarea#message").val();
$.ajax({
url: "send.php",
type: "POST",
data: {
firstname: firstname,
lastname: lastname,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$('.contact-alert').html("<div class='alert alert-success'>");
$('.contact-alert > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'><i class='fa fa-times'></i></button><strong>Your message has been sent.</strong></div>");
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('.contact-alert').html("<div class='alert alert-danger'>");
$('.contact-alert > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'><i class='fa fa-times'></i></button><strong>Sorry, it seems that my mail server is not responding. Please try again later!</strong></div>");
//clear all fields
$('#contactForm').trigger("reset");
},
})
}
});
});
</script>
The send.php file.. I also have downloaded class.phpmailer.php and it's in the same location as the send.php
if(isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['email']) && isset($_POST['message'])) {
require("class.phpmailer.php"); //you have to download this plugin from github.com - link is in documentation
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->From = $email;
$mail->CharSet = "UTF-8";
$mail->FromName = $email;
$mail->addAddress("adomas2006#gmail.com");
$mail->IsHTML(true);
$mail->Subject = $name." sent you an email";
$mail->Body = $message."<br><br>".$name."<br>".$email."<br>".$phone."<br>";
$mail->send();

Cannot read property 'jqBootstrapValidation'

I have some errors here in my jQuery. I just want to know what is the problem of this. It can validate my contact form and it says "Message has been sent". I check my email inbox but I have not receive the message.
.
contact_me.js:3 Uncaught TypeError: Cannot read property 'jqBootstrapValidation' of null
(anonymous function) # contact_me.js:3
j # jquery.js:2
k.fireWith # jquery.js:2
m.extend.ready # jquery.js:2
J # jquery.js:2
Here is my code:
<form name="sentMessage" id="contactForm" action="mail/contact_me.php" >
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button type="submit" class="btn btn-xl">Send Message</button>
</div>
</div>
</form>
This is my contact_me.js
$(function() {
$("input,textarea").jqBootstrapValidation(
{
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "././mail/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
The error begin in the beginning ".jqBootstrapValidation..."

all parameters correct? but Contact Form submitting, but not sending email through [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to set up a form on my site to allow the standard "Contact me". But when it is filled out and submitted, nothing comes through to my mail. I have done a :
<?php
mail('info#ashlutonphotography.com.au','Test mail','The mail function is working!');
echo 'Mail sent!';
?>
Which works fine, but my form doesnt work with this php:
<?php
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$_email='info#ashlutonphotography.com.au';
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply#ashlutonphotography.com.au";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
This is the contact_me.js
$(function() {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
// Prevent spam click and default submit behaviour
$("#btnSubmit").attr("disabled", true);
event.preventDefault();
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "././mail/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
message: message
},
cache: false,
success: function() {
// Enable button & show success message
$("#btnSubmit").attr("disabled", false);
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
// When clicking on Full hide fail/success boxes
$('#name').focus(function() {
$('#success').html('');
});`
With this being the HTML:
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Contact us</h2>
<p><img src="PENCIL.png" width="709" height="93" alt="pen"></p>
<p> </p>
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form name="sentMessage" id="contactForm" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-success btn-lg">Send</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
Any ideas folks?
Please and thank you.
Ash
From your code:
mail($to,$email_subject,$email_body,$headers);
There is no $to var.
On your php script I don't see where the $to variable was assigned maybe you meant $_email

Categories