Form submission, just can't stop refresh - javascript

So I am very new to this stuff and i have a specific problem I just can't fix. I have searched all over, and I have tried loads of solutions to no avail. I'm sure I'm missing something or have something in the wrong order but I just need some guidance.
I have a simple form in my website and I can't stop it refreshing the page on submit. There's some php validating happening also.
Here's a link to the website: www.nathanchapmanphotography.co.uk
Any help would be massively appreciated.
$("form").submit(function() {
var error = "";
var success = "";
var fail = "";
if ($("#name").val() == "") {
error += "name required<br>";
}
if ($("#email").val() == "") {
error += "email required<br>";
}
if ($("#message").val() == "") {
error += "message required<br>";
}
if (error != "") {
$("#error").html(error);
$("#success").html(success);
$("#fail").html(fail);
return false;
}
else {
sendContactForm();
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
<p class="form-p">name:
<input class="input" type="text" id="name" name="name">
</p>
<p class="form-p">email:
<input class="input" type="email" id="email" name="email">
</p>
<p class="form-p">message:
<br/>
<textarea id="message" cols="40" rows="7" name="message"></textarea>
</p>
<button type="submit" id="submit">submit</button>
<div id="error">
<? echo $error; ?>
</div>
<div id="success">
<? echo $success; ?>
</div>
<div id="fail">
<? echo $fail; ?>
</div>
</form>

You need to prevent the default action of the submit event:
$("form").submit(function(event) { // capture the function's event here
event.preventDefault(); // use the captured event here
// rest of your code...
EDIT: from the OP's website after the change -

I'm not 100% sure what you are looking for,..however,
If sendContactForm does the posting of the form data and you just don't want the submit to happen automatically you can make the button of type button and bind to it for the validation.
$(document).ready(function() {
function sendContactForm(){
console.log('sendContactForm');
};
$('#submit').on('click', function() {
var error = "";
var success = "";
var fail = "";
if ($("#name").val() === "") {
error += "name required<br>";
}
if ($("#email").val() === "") {
error += "email required<br>";
}
if ($("#message").val() === "") {
error += "message required<br>";
}
if (error !== "") {
$("#error").html(error);
$("#success").html(success);
$("#fail").html(fail);
} else {
sendContactForm();
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
<p class="form-p">name:
<input class="input" type="text" id="name" name="name">
</p>
<p class="form-p">email:
<input class="input" type="email" id="email" name="email">
</p>
<p class="form-p">message:
<br/>
<textarea id="message" cols="40" rows="7" name="message"></textarea>
</p>
<button type="button" id="submit">submit</button>
<div id="error">
<? echo $error; ?>
</div>
<div id="success">
<? echo $success; ?>
</div>
<div id="fail">
<? echo $fail; ?>
</div>
</form>
or,..if you want to only have more control over the submit itself without the page refreshing, you can use ajax. Similar to this:
var $form = $("form");
$.ajax({
data: $form.serialize(),
url: $form[0].action,
type: 'post',
dataType: 'json',
success: function(data) {
sendContactForm();
}
});
$(document).ready(function() {
function postData() {
var $form = $("form");
$.ajax({
data: $form.serialize(),
url: $form[0].action,
type: 'post',
dataType: 'json',
success: function(data) {
sendContactForm();
}
});
}
function sendContactForm() {
console.log('sendContactForm');
}
$('#submit').on('click', function() {
var error = "";
var success = "";
var fail = "";
if ($("#name").val() === "") {
error += "name required<br>";
}
if ($("#email").val() === "") {
error += "email required<br>";
}
if ($("#message").val() === "") {
error += "message required<br>";
}
if (error !== "") {
$("#error").html(error);
$("#success").html(success);
$("#fail").html(fail);
} else {
postData();
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="http://yourtageturl">
<p class="form-p">name:
<input class="input" type="text" id="name" name="name">
</p>
<p class="form-p">email:
<input class="input" type="email" id="email" name="email">
</p>
<p class="form-p">message:
<br/>
<textarea id="message" cols="40" rows="7" name="message"></textarea>
</p>
<button type="button" id="submit">submit</button>
<div id="error">
<? echo $error; ?>
</div>
<div id="success">
<? echo $success; ?>
</div>
<div id="fail">
<? echo $fail; ?>
</div>
</form>

So I managed to sort it using both your suggestions, thanks so much. The issue was that the function I found online to send the data actually wasn't doing anything. I have it running perfectly now, no server side validation but that's for another day. If your interested, here's the revised code.
$("form").submit(function(e) {
e.preventDefault();
var error = "";
var success = "Thank you for your message!<br/>I'll get back to you shortly."
var url = "index.php"; // the script where you handle the form input.
if ($("#name").val() == "") {
error += "name required<br>";
}
if ($("#email").val() == "") {
error += "email required<br>";
}
if ($("#message").val() == "") {
error += "message required<br>";
}
if (error != "") {
$("#error").html(error);
//alert("We need your" + error + "please!" );
}
else {
$.ajax({
type: "POST",
url: url,
data: $("form").serialize(), // serializes the form's elements.
success: function(data)
{
$("#success").html(success);
$("#error").html(error);
}
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
<p class="form-p">name:
<input class="input" type="text" id="name" name="name">
</p>
<p class="form-p">email:
<input class="input" type="email" id="email" name="email">
</p>
<p class="form-p">message:
<br/>
<textarea id="message" cols="40" rows="7" name="message"></textarea>
</p>
<button type="submit" id="submit">submit</button>
<div id="error">
<? echo $error; ?>
</div>
<div id="success">
<? echo $success; ?>
</div>
<div id="fail">
<? echo $fail; ?>
</div>
</form>

Related

Submit button does not work, how to make it work? (code provided in description)

I am trying to make this code work, and have tried many things (some of which I also found in StackOverflow), but nothing works. When I click on "Send Message" (submit button to send the contact info), absolutely nothing happens.
HTML:
<!-- CONTACT FORM -->
<form method="post" class="wow fadeInUp" id="contact-form">
<!-- IF MAIL SENT SUCCESSFUL -->
<h6 class="text-success">Your message has been sent successfully.</h6>
<!-- IF MAIL NOT SENT -->
<h6 class="text-danger">E-mail must be valid and message must be longer than 1 character.</h6>
<div class="col-md-6 col-sm-6">
<input type="text" class="form-control" id="cf-name" name="name" placeholder="Full name">
</div>
<div class="col-md-6 col-sm-6">
<input type="email" class="form-control" id="cf-email" name="email" placeholder="Email address">
</div>
<div class="col-md-12 col-sm-12">
<input type="text" class="form-control" id="cf-subject" name="subject" placeholder="Subject">
<textarea class="form-control" rows="6" id="cf-message" name="message" placeholder="Message"></textarea>
<button type="submit" class="form-control" id="cf-submit" name="submit">Send Message</button>
</div>
</form>
JS:
// CONTACT FORM
$("#contact-form").submit(function (e) {
e.preventDefault();
var name = $("#cf-name").val();
var email = $("#cf-email").val();
var subject = $("#cf-subject").val();
var message = $("#cf-message").val();
var dataString = 'name=' + name + '&email=' + email + '&subject=' + subject + '&message=' + message;
function isValidEmail(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
if (isValidEmail(email) && (message.length > 1) && (name.length > 1)) {
$.ajax({
type: "POST",
url: "contact.php"
data: dataString,
success: function () {
$('.text-success').fadeIn(1000);
//$('.text-danger').fadeOut(500);
}
});
}
else {
$('.text-danger').fadeIn(1000);
//$('.text-success').fadeOut(500);
}
return false;
});
contact.php:
<?php
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) &&
isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
// detect & prevent header injections
$test = "/(content-type|bcc:|cc:|to:)/i";
foreach ( $_POST as $key => $val ) {
if ( preg_match( $test, $val ) ) {
exit;
}
}
mail( "davidchau#gmail.com", "Contact Form: ". $_POST['name'], $_POST['subject'], $_POST['message'], "From:" . $_POST['email'] );
}
?>
I have the JS script inside the body of the HTML (at the end of the body). Any help is greatly appreciated.

How to set cookies for remember me checkbox after login in codeigniter?

view:
<script>
$(document).ready(function(){
$("#login").click(function(e){
e.preventDefault();
elogin = $("#elogin").val();
plogin = $("#plogin").val();
remember_me = $("#remember_me").val();
$.ajax({
type:"POST",
data:{"elogin":elogin,"plogin":plogin,"remember_me":remember_me},
url:"<?php echo base_url(); ?>login_redirect",
success: function(data) {
if (typeof data !== 'object') {
data = JSON.parse(data);
}
if (data.redirect)
{
window.location.replace(data.redirect);
}
else
{
$(".login_success").html('<p>' + data.error + '</p>');
}
}
});
});
});
</script>
<div class="login_success"></div>
<label class="control-label">Your Email</label>
<input class="form-control" placeholder="" type="email" id="elogin">
<label class="control-label">Your Password</label>
<input class="form-control" placeholder="" type="password" id="plogin">
<input name="optionsCheckboxes" id="remember_me" type="checkbox"> Remember Me
<button class="btn btn-lg btn-primary full-width" id="login">Login</button>
Controller:
public function login_redirect()
{
$email = $this->input->post('elogin');
$password = $this->input->post('plogin');
$remember = $this->input->post('remember_me');
$this->db->select('*');
$this->db->from('user');
$where = "email='".$email."' and password='".$password."' and status='1'";
$this->db->where($where);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$result = $query->result_array();
$this->session->set_userdata('user_id',$result);
if (!isset($_POST))
{
header ("Location:".base_url()."thankyou");
}
else
{
echo json_encode(array('redirect' => base_url().'thankyou'));
}
}
else
{
echo json_encode(array('error' => 'Wrong email or password or may be your account not activated.'));
}
}
In this code, I simply create a login form and do login via jquery ajax which is successfully working. Now, I want to set cookie after successful login via jquery ajax and cookie available only for one day after the day cookie expires. So, How can I do this? Please help me.
Thank You
View Page - Login.php :
<input type="email" class="form-control" name="email" placeholder="Enter Email" value="<?php if(isset($_COOKIE["email"])) { echo $_COOKIE["email"]; } ?>" required>
<input type="checkbox" name="remember" <?php if(isset($_COOKIE["email"])) { ?> checked <?php } ?> > Remember Me
Controller - LoginController.php
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$remember = $this->input->post('remember');
if(!empty($remember)) {
setcookie ("email",$email,time()+ 3600);
setcookie ("password",$password,time()+ 3600);
}else{
if(isset($_COOKIE["email"] && isset($_COOKIE["password"])) ){
setcookie ("email",""); setcookie ("password","");
}
}

Adding Comment in textarea and displaying text with Edit and delete buttons

I have textarea with save and cancel buttons for updating textarea text in mysql DB.
Initially my MYSQL db
ID text
1 NULL
If i enter some text in textarea i'm updating my mysql db text with entered value currently i'm able to achieve it but my requirment is once i entered text in textarea it should update my db and that text value should display with EDIT and DELETE buttons.
on clicking EDIT button it should open up textarea with save and cancel buttons. can somebody aid me out how to achieve it Thanks!
http://jsfiddle.net/a32yjx0k/
HTML
<div id="b_news">
<form method="post" action="">
</div>
<div class="breaking_news_content">
<div>
<form method="post" action="">
<div>
<div>
<textarea id="breaking_news_text" class="breaking_news_text" rows="6" cols="50" placeholder="Add text here..." required></textarea>
</div>
</div>
<div>
<input type="submit" class=" save_breaking_news" value="Save Changes"/>
<input type="submit" value="Cancel" class=" breaking_news_cancel">
</div>
</form>
</div>
</div>
</form>
</div>
JQUERY
$(function(){
$(".save_breaking_news").click(function(){
var textcontent = $('.breaking_news_text').val();
if(textcontent == '')
{
alert("Enter Some Text...");
$('.breaking_news_text').focus();
}
else
{
$.ajax({
type: "POST",
url: "index.php",
data:{
textcontent:textcontent
},
success:function(response){
alert('breaking news successfully updated');
}
});
}
return false;
});
});
PHP
<?php
if(isset($_POST['textcontent']))
{
$breaking_news = mysqli_real_escape_string($con, $_POST['textcontent']);
$sql = "update breakingnews set text='".$breaking_news."'";
$result = mysqli_query($con, $sql);
}
?>
$(function(){
$(".save_breaking_news").click(function(){
var textcontent = $('.breaking_news_text').text();
if(textcontent == '')
{
alert("Enter Some Text...");
$('.breaking_news_text').focus();
}
else
{
$.ajax({
type: "POST",
url: "index.php",
data:{
textcontent:textcontent
},
success:function(response){
alert('breaking news successfully updated');
}
});
}
return false;
});
});
To get textbox use (class/id).text();
Your DIV
<div id="b_news">
<form method="post" action="">
</div>
<div class="breaking_news_content">
<div>
<form method="post" action="">
<div>
<div>
<textarea id="breaking_news_text" class="breaking_news_text" rows="6" cols="50" placeholder="Add text here..." required></textarea>
</div>
</div>
<div>
<input type="hidden" id="post_ID" value="2"/>
<input type="button" class=" save_breaking_news" value="Save Changes"/>
<input type="button" value="Cancel" class=" breaking_news_cancel">
</div>
</form>
</div>
</div>
</form>
</div>
YOUR SCRIPT SHOULD BE LIKE THIS
$(function(){
$(".save_breaking_news").click(function(){
var textcontent = $('.breaking_news_text').text();
if(textcontent == '')
{
alert("Enter Some Text...");
$('.breaking_news_text').focus();
}
else
{
var postID=$("#post_ID").val();
$.ajax({
url: 'index.php',
type: 'post',
data: 'textcontent=' + drvNo+"id="+postID,
success:function(response){
alert('breaking news successfully updated');
}
});
}
return false;
});
});
YOUR PHP CODE FOR UPDATE
<?php
if(isset($_POST['textcontent']))
{
$breaking_news = mysqli_real_escape_string($con, $_POST['textcontent']);
$sql = "update breakingnews set text='".$breaking_news."' Where id='".$_POST['id']."'";
$result = mysqli_query($con, $sql);
}
?>
AND IF YOU WANT TO INSERT POST YOUR CODE SHOULD BE LIKE THIS:
<?php
if(isset($_POST['textcontent']) && !isset($_POST['id']))
{
$breaking_news = mysqli_real_escape_string($con, $_POST['textcontent']);
$sql = "insert into <TBL NAME> `text` values ('".$_POST['textcontent']."')";
$result = mysqli_query($con, $sql);
}
?>
Your code everything is fine. Instead of calling function use .keyup() function in Jquery.
$("#breaking_news_text").keyup(function(){
var textcontent = $('.breaking_news_text').val();
if(textcontent == '')
{
alert("Enter Some Text...");
$('.breaking_news_text').focus();
}
else
{
alert(textcontent);
$.ajax({
type: "POST",
url: "index.php",
data:
{
textcontent:textcontent
},
success:function(response)
{
alert('breaking news successfully updated');
}
});
}
return false;
});
and when you going to cancel please use input type="reset"
<input type="reset" value="Cancel" class=" breaking_news_cancel">

Contact Form missing (send.php file)

Hi i would just like to ask some help regarding my contact form because I do not know how to create a php script to run on submit my form (send.php). Here my code for the Contact Form:
<div class="contact_form_holder">
<form id="contact" class="row" name="form1" method="post" action="#">
<div class="span4">
<label>Nom</label>
<input type="text" class="full" name="name" id="name" />
</div>
<div class="span4">
<label>Email <span class="req">*</span></label>
<input type="text" class="full" name="email" id="email" />
<div id="error_email" class="error">Please check your email</div>
</div>
<div class="span8">
<label>Message <span class="req">*</span></label>
<textarea cols="10" rows="10" name="message" id="message" class="full"></textarea>
<div id="error_message" class="error">Please check your message</div>
<div id="mail_success" class="success">Thank you. Your message has been sent.</div>
<div id="mail_failed" class="error">Error, email not sent</div>
<p id="btnsubmit">
<input type="submit" id="send" value="Send" class="btn btn-large" /></p>
</div>
</form>
</div>
Here my Javascript:
$(document).ready(function(){
$("#send").click(function(){
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var error = false;
if(email.length == 0 || email.indexOf("#") == "-1" || email.indexOf(".") == "-1"){
var error = true;
$("#error_email").fadeIn(500);
}else{
$("#error_email").fadeOut(500);
}
if(message.length == 0){
var error = true;
$("#error_message").fadeIn(500);
}else{
$("#error_message").fadeOut(500);
}
if(error == false){
$("#send").attr({"disabled" : "true", "value" : "Loading..." });
$.ajax({
type: "POST",
url : "send.php",
data: "name=" + name + "&email=" + email + "&subject=" + "You Got Email" + "&message=" + message,
success: function(data){
if(data == 'success'){
$("#btnsubmit").remove();
$("#mail_success").fadeIn(500);
}else{
$("#mail_failed").html(data).fadeIn(500);
$("#send").removeAttr("disabled").attr("value", "send");
}
}
});
}
return false;
});
});
I'm guessing i need the url : "send.php", Thank you so much in advance.
you should give your action page where you are receiving your data. your code could be like this..
<form id="contact" class="row" name="form1" method="post" action="send.php">
First of all actually submits the surrounding form.
Use Because it does nothing next to submit.
Secondly in your ajax call you have written :
success: function(data){
if(data == 'success'){
$("#btnsubmit").remove();
$("#mail_success").fadeIn(500);
}
});
The if condition is invalid if the value of data is not 'success'. I don't know whether it is returning success or not.
So lastly change input type submit to input type button .. hope it will work..
Make a file name with "send.php" in the same root where it's html file exist.
and some php code here ...
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: webmaster#example.com" . "\r\n" .
//mail(to,subject,message,headers,parameters);
mail($email,$subject,$message,$headers);

HTML/JS Contact Form Not Sending or Showing Error Message

I have a contact form that I can't seem to send to my Gmail account. It's different from all the contact forms I've seen because the error message is within the HTML. Nothing happens when the submit button is pressed (no email, no error or success message). Please be gentle for I am somewhat new to PHP. I just need some help please.
The HTML
<div class="contactForm">
<div class="successMessage alert alert-success alert-dismissable" style="display: none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Thank You! E-mail was sent.
</div>
<div class="errorMessage alert alert-danger alert-dismissable" style="display: none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Oops! An error occured. Please try again later.
</div>
<form class="liveForm" role="form" action="form/send.php" method="post" data-email-subject="Contact Form" data-show-errors="true" data-hide-form="false">
<fieldset>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Name <span>(Required)</span></label>
<input type="text" required name="field[]" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Email <span>(Required)</span></label>
<input type="email" required name="field[]" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Subject</label>
<input type="text" name="field[]" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Message <span>(Required)</span></label>
<textarea name="field[]" required class="form-control" rows="5"></textarea>
</div>
</div>
</div>
<input type="submit" class="btn btn-primary" value="Send Message">
</fieldset>
</form>
</div>
</div>
The JS
/**
* Contact Form
*/
jQuery(document).ready(function ($) {
"use strict";
$ = jQuery.noConflict();
var debug = false; //show system errors
$('.liveForm').submit(function () {
var $f = $(this);
var showErrors = $f.attr('data-show-errors') == 'true';
var hideForm = $f.attr('data-hide-form') == 'true';
var emailSubject = $f.attr('data-email-subject');
var $submit = $f.find('[type="submit"]');
//prevent double click
if ($submit.hasClass('disabled')) {
return false;
}
$('[name="field[]"]', $f).each(function (key, e) {
var $e = $(e);
var p = $e.parent().find("label").text();
if (p) {
var t = $e.attr('required') ? '[required]' : '[optional]';
var type = $e.attr('type') ? $e.attr('type') : 'unknown';
t = t + '[' + type + ']';
var n = $e.attr('name').replace('[]', '[' + p + ']');
n = n + t;
$e.attr('data-previous-name', $e.attr('name'));
$e.attr('name', n);
}
});
$submit.addClass('disabled');
$f.append('<input class="temp" type="hidden" name="email_subject" value="' + emailSubject + '">');
$.ajax({
url: $f.attr('action'),
method: 'post',
data: $f.serialize(),
dataType: 'json',
success: function (data) {
$('span.error', $f).remove();
$('.error', $f).removeClass('error');
$('.form-group', $f).removeClass('has-error');
if (data.errors) {
$.each(data.errors, function (i, k) {
var input = $('[name^="' + i + '"]', $f).addClass('error');
if (showErrors) {
input.after('<span class="error help-block">' + k + '</span>');
}
if (input.parent('.form-group')) {
input.parent('.form-group').addClass('has-error');
}
});
} else {
var item = data.success ? '.successMessage' : '.errorMessage';
if (hideForm) {
$f.fadeOut(function () {
$f.parent().find(item).show();
});
} else {
$f.parent().find(item).fadeIn();
$f[0].reset();
}
}
$submit.removeClass('disabled');
cleanupForm($f);
},
error: function (data) {
if (debug) {
alert(data.responseText);
}
$submit.removeClass('disabled');
cleanupForm($f);
}
});
return false;
});
function cleanupForm($f) {
$f.find('.temp').remove();
$f.find('[data-previous-name]').each(function () {
var $e = jQuery(this);
$e.attr('name', $e.attr('data-previous-name'));
$e.removeAttr('data-previous-name');
});
}
});
The PHP
<?php
// Contact subject
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Enter your email address
$to ='divagraphicsinc#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
?>
<?php
$ajax = (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$ajax = true;
//we do not allow direct script access
if (!$ajax) {
//redirect to contact form
echo "Please enable Javascript";
exit;
}
require_once "config.php";
//we set up subject
$mail->Subject = isset($_REQUEST['email_subject']) ? $_REQUEST['email_subject'] : "Message from site";
//let's validate and return errors if required
$data = $mail->validateDynamic(array('required_error' => $requiredMessage, 'email_error' => $invalidEmail), $_REQUEST);
if ($data['errors']) {
echo json_encode(array('errors' => $data['errors']));
exit;
}
$html = '<div style="width: 640px; font-size: 11px;">
<h2>' . $mail->Subject . '</h2><ul>
';
foreach ($data['fields'] as $label => $val) {
$html .= '<li>' . $label . ': ' . $val . '</li>';
}
$html .= '</ul></div>';
$mail->setup($html, $_REQUEST, array());
$result = array('success' => 1);
if (!$mail->Send()) {
$result['success'] = 0;
}
echo json_encode($result);
exit;

Categories