I've used This tutorial to create a contact form on my site. Unfortunately, the amount of fields are not sufficient for me, so I added a company and subject field to the form.
However.. I can't get the mail output working with the additional fields.
Quick roundup:
html:
Each input field in the HTML is based on the following piece of lines:
<label>Subject</label>
<input type="text" id="subject" placeholder="What's up?"></div>
jQuery:
The full jQuery code is:
$(document).ready(function() {
// Contact Form
$("#contactform").submit(function(e){
e.preventDefault();
var name = $("#name").val();
var company = $("#company").val();
var email = $("#email").val();
var subject = $("#subject").val();
var text = $("#text").val();
var dataString = 'name=' + name + + '&company' + company + '&email=' + email + '&subject' + subject + '&text=' + text;
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) && (text.length > 25) && (name.length > 1)){
$.ajax({
type: "POST",
url: "js/functions.php",
data: dataString,
success: function(){
$('.success').fadeIn(1000);
}
});
} else{
$('.error').fadeIn(1000);
}
return false;
});
});
And the whole functions.php script is:
<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['text']) && 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;
}
}
//send email
mail( "myEmail@gmail.com", "New message: ".$_POST['name'], $_POST['text'], "From:" . $_POST['email'] );
}
?>
Ok, so here is where we are: The form does get submitted and I do receive a mail, however the additional fields ($('#subject') and $('#company')) do not appear within the Email.
So there are two things I want to achieve within this thread: 1) The fields 'subject' & 'company' appear within the e-mail 2) The mail I receive has the following lay-out:
Subject line: 'You received a new message: ' + 'subject'
Message body: 'You received a new mail via your contact form<br/>
Name: 'name'
Company: 'company'
Reply to: 'email'
Subject: 'subject'
Message: 'text'
I've tried to adjust line - Because I thought that would solve it all:
//send email
mail( "myEmail@gmail.com", "New message: ".$_POST['name'], $_POST['text'], "From:" . $_POST['email'] );
But the luck wasn't with me. So based on the above pieces of code, what lines do I need to adjust and what do I need to add?
Thanks very much guys!