使用PHP和Jquery将表单输入字段添加到电子邮件中 [英] Add form input fields to E-mail with PHP and Jquery

查看:133
本文介绍了使用PHP和Jquery将表单输入字段添加到电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用教程在我的网站上创建联系表地点.不幸的是,字段的数量对我来说还不够,因此我在表格中添加了公司和主题字段.

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.

快速汇总:

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;
});

});

整个functions.php脚本是:

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'] );

}
?>

好的,这就是我们的位置:表单确实已提交,我也收到了邮件,但是其他字段($('#subject')和$('#company'))不会出现在电子邮件.

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.

因此,我想在此线程中实现两件事: 1)字段主题"和"公司"出现在电子邮件中 2)我收到的邮件具有以下布局:

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?

非常感谢大家!

推荐答案

编辑JS文件以反映此情况: JS:

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;
});

});

编辑PHP文件以体现这一点:

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'] );

}
?>

这篇关于使用PHP和Jquery将表单输入字段添加到电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆