I have a simple script for a form submit using javascript / jQuery. For some reason, it works the first time (for 10 days) now I got this error in firefox (console):
POST https://mydomain.de/wp-admin/admin-ajax.php
Status 403 Forbidden
Version HTTP/1.1
Übertragen 567 B (2 B Größe)
Referrer Policy strict-origin-when-cross-origin
Code in functions.php
// e-Mail handling
// if you want only logged in users to access this function use this hook
add_action('wp_ajax_mail_before_submit', 'mycustomtheme_send_mail_before_submit');
// if you want none logged in users to access this function use this hook
add_action('wp_ajax_nopriv_mail_before_submit', 'mycustomtheme_send_mail_before_submit');
// if you want both logged in and anonymous users to get the emails, use both hooks above
function mycustomtheme_send_mail_before_submit(){
check_ajax_referer('my_email_ajax_nonce');
if ( isset($_POST['action']) && $_POST['action'] == "mail_before_submit" ){
// catch vars for mail
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];
$anschrift = $_POST['anschrift'];
$telefon = $_POST['tel'];
$wohnort = $_POST['wohnort'];
$klasse = $_POST['klasse'];
$customer_mail = $_POST['toemail'];
$birthdate = $_POST['birthdate'];
$headers = array('Content-Type: text/html; charset=UTF-8','From: Fahrschule Website <[email protected]>');
$msg_body = '<h3>Anmeldung von '.$firstname.' '.$lastname.' - '.$klasse.'</h3>';
$msg_body.= '<p>Der Fahrschüler meldet sich mit folgenden Daten an:<br><br>';
$msg_body.= $firstname.' '.$lastname.'<br>'.$anschrift.'<br>'.$wohnort.'<br><br>Geburtsdatum und -ort: '.$birthdate.'<br><br>Telefon: '.$telefon.'<br>E-Mail: '.$customer_mail;
$msg_body.= '</p>';
//send email wp_mail( $to, $subject, $message, $headers, $attachments ); ex:
wp_mail('[email protected]','Anmeldung Fahrerlaubnis',$msg_body, $headers);
echo 'Anmeldung gesendet';
wp_die();
}
echo 'Fehler beim senden';
die();
}
Code from js-file
function send_form() {
jQuery('.alert-info').hide();
//jQuery('.alert-info').fadeIn();
var myModal = document.getElementById('modalRegister');
var err_firstname = document.getElementById('err_firstname');
//jQuery('#err_firstname').hide();
var auswahl = jQuery('#test-auswahl').val();
var klasse = optionRequired('test-auswahl', 'Klasse');
var fn = checkEmpty('firstname', 'Vorname');
var ln = checkEmpty('lastname', 'Nachname');
var an = checkEmpty('anschrift', 'Straße / Nr');
var an = checkEmpty('plz_ort', 'Plz / Wohnort');
var tel = checkEmpty('tel', 'Telefon');
var tel = checkEmpty('geb_dat', 'Geburtsdatum / Ort');
var mailf = validateEmail('email', "E-Mail Adresse");
if(fn && ln && an && tel && mailf && klasse) {
jQuery('.alert-info').html('Formular gesendet! Anmeldung für ' auswahl);
jQuery('.alert-info').fadeIn();
jQuery('.inhalt').hide();
jQuery('.modal-footer .btn-primary').hide();
// Mail testen
var data = {
action: 'mail_before_submit',
toemail: jQuery('#fbfrm_email').val(), // change this to the email field on your form
lastname: jQuery('#fbfrm_lastname').val(),
firstname: jQuery('#fbfrm_firstname').val(),
anschrift: jQuery('#fbfrm_anschrift').val(),
wohnort: jQuery('#fbfrm_plz_ort').val(),
tel: jQuery('#fbfrm_tel').val(),
klasse: jQuery('#test-auswahl').val(),
_ajax_nonce: jQuery('#my_email_ajax_nonce').data('nonce'),
};
jQuery.post(window.location.origin "/wp-admin/admin-ajax.php", data, function(response) {
console.log('Got this from the server: ' response);
});
}
}
CodePudding user response:
Thank you very much - it works :) I'd changed it to: check_ajax_referer('my_email_ajax_nonce', false, false);