Answer the question
In order to leave comments, you need to log in
Wordpress form with ajax handler, white screen when button is clicked, what's wrong?
I would like to know - I'm wrong in the code or another typo. It goes when clicking on site-name.com/wp-admin/admin-ajax.php?action=send_mail , but a white screen. In console Unchecked runtime.lastError: The message port closed before a response was received. site-name.com/wp-admin/admin-ajax.php?action=send_mail
HTML form:
<form id="pp_booking_form" method="post" action="<?php echo admin_url('admin-ajax.php?action=send_mail') ?>" >
<input type="hidden" id="action" name="action" value="pp_booking_mailer">
<input type="hidden" id="tour_title" name="tour_title" value="East Europe">
<input type="hidden" id="tour_url" name="tour_url" value="index.php">
<div class="one_half">
<label for="first_name">First Name</label>
<input id="first_name" name="first_name" type="text" class="required_field" required>
</div>
<div class="one_half last">
<label for="last_name">Last Name</label>
<input id="last_name" name="last_name" type="text" class="required_field" required>
</div>
<br class="clear">
<br>
<div class="one_half">
<label for="email">Email</label>
<input id="email" name="email" type="text" class="required_field" required>
</div>
<div class="one_half last">
<label for="phone">Phone</label>
<input id="phone" name="phone" type="text">
</div>
<br class="clear">
<br>
<div class="one">
<label for="message">Additional Message</label>
<textarea id="message" name="message" rows="7" cols="10" required></textarea>
</div>
<br class="clear">
<div class="one">
<p>
<input id="booking_submit_btn" type="submit" value="Book By Email">
</p>
</div>
</form>
jQuery(document).ready(function($) {
var form = $('#pp_booking_form');
var action = form.attr('action');
form.on('submit', function(event) {
var formData = {
first_name: $('#first_name').val(),
last_name: $('#last_name').val(),
email: $('#email').val(),
phone: $('#phone').val(),
message: $('#message').val()
};
$.ajax({
url: action,
type: 'POST',
data: formData,
error: function() {
form.html("Ваш тур не забронирован");
};
sussess: function() {
form.html("Ваш тур забронирован");
}
});
event.preventDefault();
});
});
wp_enqueue_script( 'form_js', get_template_directory_uri() . '/assets/js/form.js', null, ['jquery'], true);
add_action( 'wp_ajax_send_mail', 'send_mail' );
add_action( 'wp_ajax_nopriv_send_mail', 'send_mail' );
function send_mail() {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$to = get_option( 'admin_email');
$subject = 'Письмо без темы';
remove_all_filters( 'wp_mail_from');
remove_all_filters( 'wp_mail_from_name' );
$headers = array(
'From: Me Myself <[email protected]>',
'content-type: text/html',
'Cc: John Q Codex <[email protected]>',
'Cc: [email protected]', // тут можно использовать только простой email адрес
);
wp_mail( $to, $subject, $message, $headers );
wp_die();
};
<input type="hidden" id="action" name="action" value="pp_booking_mailer">
<input type="hidden" id="tour_title" name="tour_title" value="East Europe">
<input type="hidden" id="tour_url" name="tour_url" value="index.php">
Answer the question
In order to leave comments, you need to log in
Turn on the display of errors on the screen and you will see all the jambs in the script
The white screen is most likely a fatal error, which stops the execution of the script
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question