wp_mail()+ Ajax + ValidationEngine + $ wpdb-> insert

时间:2017-02-14 11:59:54

标签: php mysql ajax wordpress forms

我决定问这个问题,因为没有简单的答案。 我的home.php中有一个联系表格,如下所示:

HTML:

<form id="js-calculator" name="calculator" action="" method="post">
     <input type="email" name="email" id="email" placeholder="E-mail" />
     <input type="tel" name="phone" id="phone" placeholder="Phone" />
     <textarea name="message" id="message" placeholder="Message"></textarea>
     <label for="accept"><input class="" type="checkbox" id="accept" name="accept" /> I agree to terms and conditions</label>
     <button type="submit" id="send__btn">Wyślij wycenę</button></p>
</form>

JavaScript的:

jQuery(document).ready(function ($) {
  $('#js-calculator').submit(function (e) {
    e.preventDefault();
    var $this = $(this);
    $.ajax({
      url: '<?php echo admin_url("admin-ajax.php") ?>',
      type: 'post',
      dataType: 'JSON',
      data: $this.serialize()
      }
    });
  });
});

PHP中的functions.php

  // Function to send emails
  function sendMail () {
    $subject = 'Automatic evaluation';
    $headers = 'From: My Website Contact Form <contact@mysite.com>';
    $send_to = "office@mysite.com, ". $_POST['email'];
    $subject = "Evaluation for ". $_POST['name'];
    $message = "Message from ".$_POST['message'];
  }
  add_action('wp_ajax_sendhtmlmail', 'sendMail');
  add_action('wp_ajax_nopriv_sendhtmlmail', 'sendMail');

  add_filter( 'wp_mail_content_type', 'set_content_type' );
  function set_content_type( $content_type ) {
    return 'text/html';
  }

  // Function to update DB
  function addCustomer(){
  global $wpdb;

  $phone = $_POST['phone'];
  $email = $_POST['email'];
  $accept = $_POST['accept'];

  if($wpdb->insert('customers',array(
    'phone'=>$phone,
    'email'=>$email,
    'accept'=>$accept
  ))===FALSE){
  echo "Error";
} else {
  $wpdb->insert_id;    
}
die();
}
add_action('wp_ajax_addCustomer', 'addCustomer');
add_action('wp_ajax_nopriv_addCustomer', 'addCustomer');

我想要达成的目标是: 1.向客户端和网站管理员发送HTML电子邮件, 2.将客户端详细信息添加到数据库,检查是否存在电 3.拥有安全的连接和数据流;

现在我不知道我做错了什么......任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

你几乎就在那里,有几件事:

在你的JS ajax调用中,你需要指定要调用的动作,这里是修改后的调用:

jQuery(document).ready(function ($) {
    $('#js-calculator').submit(function (e) {
      e.preventDefault();
      var $this = $(this);
      $.ajax({
        url: '<?php echo admin_url("admin-ajax.php?action=sendhtmlmail") ?>',
        type: 'post',
        dataType: 'JSON',
        data: $this.serialize()
        }
      });
    });
  });

现在,让我们看一下php函数:

// Function to send emails
function sendMail () {
  $data = array(
    'email' = sanitize_email($_POST['email']),
    'name' = sanitize_text_field($_POST['name']),
    'message' = sanitize_text_field($_POST['message']),
  );
  $subject = 'Automatic evaluation;
  $headers = 'From: My Website Contact Form <contact@mysite.com>';
  $send_to = "office@mysite.com, ". $data['email'];
  $subject = "Evaluation for ". $data['name'];
  $message = "Message from ".$data['message'];
  wp_mail($send_to, $subject, $message, $headers);
  addCustomer($data);
  // TODO: Fill in this $response array with meaninfull data (check output from wp_mail and addCustomer)
  $response = array(
    'success' => true
  );
  wp_send_json($response);
}
add_action('wp_ajax_sendhtmlmail', 'sendMail');
add_action('wp_ajax_nopriv_sendhtmlmail', 'sendMail');

只是错过了wp_mail调用,并清理了输入,我删除了你的其他ajax调用,并让sendMail函数将数据传递给addCustomer函数。你会注意到一个wp_send_json调用,这是将响应发送给JS。 https://codex.wordpress.org/Function_Reference/wp_send_json

现在让我们来看看你的addCustomer函数:

// Function to update DB
function addCustomer($data){
  global $wpdb;

  $phone = $data['phone'];
  $email = $data['email'];
  $accept = $data['accept'];

  if($wpdb->insert('customers',array(
      'phone'=>$phone,
      'email'=>$email,
      'accept'=>$accept
    ))===FALSE){
    return false;
  } else {
    return $wpdb->insert_id;
  }
}

这里唯一改变的是返回,以便调用者知道它是否失败。

要处理来自用户的输入,请从wordpress和核心功能中检查此文档: https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data

或者您可以一起使用以下插件:

以下是如何使用它:https://contactform7.com/save-submitted-messages-with-flamingo/

我希望这会对你有所帮助! 的问候,

P.S wordpress问题更适合这里:https://wordpress.stackexchange.com/