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:
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>
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!
Edit JS file to reflect this: JS:
$(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;
});
});
Edit the PHP file to reflect this:
PHP:
<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['text']) && isset($_POST['company']) && isset($_POST['subject']) && 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;
}
}
$to = "myEmail@gmail.com";
$subject = 'You received a new message: ' . $_POST['subject'];
$message = "You received a new mail via your contact form\n";
$message .= "Name: " . $_POST['name'] . "\n";
$message .= "Company: " . $_POST['company'] . "\n";
$message .= "Reply to: " . $_POST['email'] . "\n";
$message .= "Subject: " . $_POST['subject'] . "\n";
$message .= "Message: " . $_POST['text'] . "\n";
//send email
mail( $to, $subject, $message, "From:" . $_POST['email'] );
}
?>
Multiple errors, first in your javascript string.
'&subject'
should be
'&subject='
note you're missing the "="
further, you never capture those two new $_POST
'd values in the PHP script.
$_POST['subject'];
$_POST['company'];
The 2nd argument of php's mail function is the subject soo...
mail( "myEmail@gmail.com", $_POST['subject'], 'You have a new message from: '.$_POST['name'].' with '.$_POST['company'].'. '. $_POST['text'], "From:" . $_POST['email'] );
Better use serialize() to get the data
var dataString = $("#contactform").serialize();
and delete this lines:
var name = $("#name").val();
var company = $("#company").val();
var subject = $("#subject").val();
var text = $("#text").val();
var dataString = 'name=' + name + + '&company' + company + '&email=' + email + '&subject' + subject + '&text=' + text;
Second, you are sending the email using:
mail( "myEmail@gmail.com", "New message: ".$_POST['name'], $_POST['text'], "From:" . $_POST['email'] );
There is no $_POSTS subject or company sended on to the email. I suppouse that the email you recived shows the "From:...." you can add the rest of the fields that you want there:
mail( "myEmail@gmail.com", "New message: ".$_POST['name'], $_POST['text'], "From:" . $_POST['email']. " with subject: ".$_POST['subject'].' and company '.$_POST['company'] );
if you look at the mail function in PHP http://www.php.net/manual/es/function.mail.php you can see what fields are what in the interface.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.