Jump to content

Recommended Posts

I'm having a bit of trouble with a PHP contact form. There are issues with the error messages not showing on the form, the form not resetting, and the "name" input appearing as "X-AuthUser: XXX123 (which happens to be my FTP user name which I'm keeping private) when the email is sent to my gmail account.

 

This is how the theme should look: http://pluto.html.themewoodmen.com/07-pluto-contact.html And this is what I have: http://divagraphics.us/test/contact.html

 
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>
Ups! 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="name" class="form-control" id="name">
</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="email" class="form-control" id="email">
</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="subject" class="form-control" id="subject">
</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="message" required class="form-control" rows="5" id="message"></textarea>
</div>
</div>
</div>
<input type="submit" class="btn btn-primary" value="Send Message">
</fieldset>
</form>

</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'];
$to = "divagraphicsinc@gmail.com";
$from = $_POST['email'];
$headers = "From: $from";
mail($to,$subject,$message,$headers)
?>
Link to comment
https://forums.phpfreaks.com/topic/287543-contact-form-not-showing-successerror/
Share on other sites

Your ajax never receives anything from you php so I think the ajax success is never getting called?

 

Change your php to this maybe 


// Contact subject
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "divagraphicsinc@gmail.com";
$from = $_POST['email'];
$headers = "From: $from";

$flag = false;
$success = mail($to,$subject,$message,$headers);

if($success)
  $flag = true;

return $flag;

Edited by Rifts
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.