getFormData() {
var data = {
name: this.refs.name.getValue(),
mail: this.refs.mail.getValue(),
text: this.refs.text.getValue()
};
this.handleSubmit(data);
},
handleSubmit(data) {
console.log("Hi from handleSubmit");
console.log(data.name);
console.log(data.mail);
console.log(data.text);
jQuery.post(
MyAjax.ajaxurl,
{
'action': 'send_message',
'name': data.name,
'email': data.mail,
'message': data.text
},
function(response){
alert('The server responded: ' + response);
}
);
},
на сервере
wp_enqueue_script( 'bundle', plugins_url( '/bundle.js', __FILE__ ), array('jquery'), '1.0', true );
wp_localize_script( 'bundle', 'MyAjax', array(
'ajaxurl' => admin_url('admin-ajax.php')
) );
add_action( 'wp_ajax_send_message', 'do_send_message' );
add_action( 'wp_ajax_nopriv_send_message', 'do_send_message' );
function do_send_message() {
if ( isset($_POST['name']) && isset($_POST['email']) &&isset($_POST['message']) ) {
$name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message'];
$send_to = "antibioticvz@gmail.com";
$subject = "Question from " . $name;
$success = wp_mail($send_to,$subject,$message);
if ($succsess) return true;
if (!$succsess) return false;
}
}