Я использую плагины Elementor Pro, «Buddyboss» и «Paid Membership Pro», но не могу найти способ использования бесплатного метода для достижения этой цели. Кажется, нужна версия «Pro».
Я пытался использовать форму Elementor Pro, затем добавил код JS и PHP, но страница все равно будет перенаправлена.
Вот коды, которые я использовал:
< pre class="snippet-code-js lang-js Prettyprint-override">
Код: Выделить всё
// [1] I USED FIRST THIS CODE ONLY JAVASCRIPT. THE ALERTBOX WILL APPEAR BUT STILL WILL REDIRECT THE PAGE
document.addEventListener('DOMContentLoaded', function() {
// Select the form using its class
const form = document.querySelector('.FormClass'); // This is the form class itself
if (form) {
form.addEventListener('submit', function(e) {
// Select the coupon field using its name attribute
const couponField = form.querySelector('input[name="FORMFIELDNAME"]'); // This is the field name element
const validCouponCode = 'MYCOUPONCODE'; // This is my coupon code
// Check if the coupon code entered is not valid
if (couponField && couponField.value !== validCouponCode) {
// Prevent form submission
e.preventDefault();
alert('Invalid coupon code.');
return false; // Ensure the form is not submitted
}
});
}
});
// [2] SINCE THE ABOVE CODE DIDN'T WORK, I TRIED TO USE A PHP CODE TOO AND JAVASCRIPT. THIS IS THE JAVASCRIPT CODE
document.addEventListener('DOMContentLoaded', function() {
// Select the form using its class
const form = document.querySelector('.FormClass');
if (form) {
form.addEventListener('submit', function(e) {
e.preventDefault(); // Prevent the form from submitting immediately
const couponField = form.querySelector('input[name="FORMFIELDNAME');
const couponCode = couponField ? couponField.value : '';
// AJAX request to validate the coupon code
fetch(ajax_object.ajax_url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'action': 'validate_coupon_code',
'form_fields[ifbfGalaCouponField]': couponCode
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
form.submit(); // Submit the form if the coupon code is valid
} else {
alert(data.data.message); // Show error message
}
});
});
}
});
// [2] THIS IS THE PHP CODE
// Validate coupon code via AJAX
function validate_coupon_code() {
// Check if the request is coming from our form
if (isset($_POST['form_fields']['FORMFIELDNAME'])) {
$entered_code = sanitize_text_field($_POST['form_fields']['FORMFIELDNAME']);
$valid_code = 'MYCOUPONCODE'; // Your valid coupon code
// Check if the entered code matches the valid code
if ($entered_code !== $valid_code) {
// Send an error response
wp_send_json_error(array('message' => 'Invalid coupon code.'));
} else {
// Send a success response
wp_send_json_success();
}
} else {
// Send an error response if the code is not set
wp_send_json_error(array('message' => 'Coupon code is required.'));
}
// Properly terminate the request
wp_die();
}
// Hook the function to handle AJAX requests
add_action('wp_ajax_validate_coupon_code', 'validate_coupon_code');
add_action('wp_ajax_nopriv_validate_coupon_code', 'validate_coupon_code');
function enqueue_custom_scripts() {
wp_enqueue_script('custom-validate-coupon', get_template_directory_uri() . '/js/custom-validate-coupon.js', array('jquery'), null, true);
wp_localize_script('custom-validate-coupon', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
Мне интересно, e.preventDefault(); если он действительно работает с Elementor.
Есть ли бесплатный плагин или другой бесплатный метод, который я могу использовать для достижения этой цели? Или вы можете помочь мне найти способ исправить код?
Большое спасибо!
Подробнее здесь: https://stackoverflow.com/questions/787 ... e-for-free