PHP表单处理程序与jQuery脚本重复电子邮件

时间:2015-10-12 08:38:00

标签: javascript php jquery forms

我有一个php函数来处理从jQuery脚本传递的表单的结果,因为表单内容也被传递到Workbooks CRM url以添加到那里的记录。

一切正常,但电子邮件发送3次除外。点击的按钮不在表单内,并且发送' isset($_POST)的值是从表单中的隐藏表单字段传递的。

我试过了:

  • 在PHP和jQuery中添加标志。
  • 在jQuery中添加警报。
  • exit("sent");功能之后添加mail()

alert()实验似乎表明jQuery不是问题,但标志似乎在PHP中表示相同!

这里是jQuery:

$(document).ready(function () {
      $("#test-vf-button1").click(function (event) {
          event.preventDefault();
            // Field values as array
            var name = $("#name").val();
            var email = $("#email").val();
            var message = $("#message").val();
            var formData = $("#wb_form").serialize();
            var url = $(location).attr('hostname');
            var pathname = $(location).attr('pathname');
            var pageUrl = url + pathname;
            console.log(pageUrl);
            $("#validate-message").empty();
            $("#confirm-message").empty();
            if (name == '' || email == '' || message == '') {
                $("#validate-message").append(" Fill in required fields");
            } else {
                // Returns successful data submission message when the entered information is stored in database.
                $.ajax({
                    type: 'POST',
                    url: "http://visual-factory.co.uk/",
                    data: formData,
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: "https://secure.workbooks.com/crm/web_leads",
                            crossDomain: true,
                            data: formData,
                            dataType: 'text',
                            success: function () {

PHP处理函数:

function vf_deliver_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['send'] ) ) {
    // sanitize form values
    $title   = sanitize_text_field( $_POST['person_lead_party']['person_personal_title'] );
    $name    = sanitize_text_field( $_POST['person_lead_party']['name'] );
    $jobrole   = sanitize_text_field( $_POST['person_lead_party']['person_job_role'] );
    $email   = sanitize_text_field( $_POST['org_lead_party']['main_location']['email']);
    $phone   = sanitize_text_field( $_POST['org_lead_party']['main_location']['telephone'] );
    $company   = sanitize_text_field( $_POST['org_lead_party']['name'] );
    $country   = sanitize_text_field( $_POST['org_lead_party']['main_location']['country'] );
    $messagecontent = esc_textarea( $_POST['vf-message'] );
    $message = "<p>Title: ".$title."</p>";
    $message .= "<p>Name of lead is: ".$name."</p>";
    $message .= "<p>Job Role: ".$jobrole."</p>";
    $message .= "<p>Email is: ".$email."</p>";
    $message .= "<p>Phone is: ".$phone."</p>";
    $message .= "<p>Company is: ".$company."</p>";
    $message .= "<p>Country is: ".$country."</p>";
    $message .= "<p>Message: ".$messagecontent.".</p>";

    // get the blog administrator's email address
    $to = get_option( 'admin_email' );
    $subject = "Form response";
    $headers = "From: $name <$email>" . "\r\n";
    mail( $to, $subject, $message, $headers ) ;
  }
}

0 个答案:

没有答案