Current File : /home/bqrcodec/test1.proid.vn/my custom code//handle-download-url-button.js
<script type="text/javascript">
jQuery(document).ready(function($) {
// Listener is on the div with class "export-user-proid-urls-button"
$(document).on('click', '.export-user-proid-urls-button', function(e) {
e.preventDefault();
var $clickedWidgetDiv = $(this); // This is the main div of the button widget
var $linkElement = $clickedWidgetDiv.find('a.elementor-button'); // Find the <a> tag within the widget
var userId;
if ($linkElement.length > 0) {
userId = $linkElement.attr('id'); // Get the ID from the <a> tag
console.log("Found <a> tag with ID (User ID):", userId);
} else {
// This case should ideally not happen if the button HTML structure is consistent
console.error("Could not find the <a> tag with class .elementor-button inside the clicked widget.");
alert('Error: Button internal structure not found. Please contact support.');
return;
}
// Validate if the extracted ID is a usable number
if (!userId || isNaN(parseInt(userId))) {
alert('Error: User ID found on link ("' + userId + '") is not a valid number. Please check button configuration and console log.');
console.log('Clicked Widget (div) HTML:', $clickedWidgetDiv[0].outerHTML);
if($linkElement.length > 0) {
console.log('Link Element (a) HTML:', $linkElement[0].outerHTML);
}
return;
}
userId = parseInt(userId); // Convert to integer
// Check if global AJAX variables are defined
if (typeof meProIDExport === 'undefined' || typeof meProIDExport.ajax_url === 'undefined' || typeof meProIDExport.nonce === 'undefined') {
alert('Error: AJAX configuration variables (meProIDExport) not found. Check functions.php.');
return;
}
// Create and submit the form for download
var form = $('<form></form>');
form.attr('method', 'POST');
form.attr('action', meProIDExport.ajax_url);
var actionInput = $('<input></input>');
actionInput.attr('type', 'hidden');
actionInput.attr('name', 'action');
actionInput.attr('value', 'me_proid_export_user_urls_action'); // Matches PHP hook
form.append(actionInput);
var userIdInput = $('<input></input>');
userIdInput.attr('type', 'hidden');
userIdInput.attr('name', 'user_id');
userIdInput.attr('value', userId);
form.append(userIdInput);
var nonceInput = $('<input></input>');
nonceInput.attr('type', 'hidden');
nonceInput.attr('name', 'security_nonce'); // Matches PHP check_ajax_referer
nonceInput.attr('value', meProIDExport.nonce);
form.append(nonceInput);
$(document.body).append(form);
form.submit();
form.remove();
});
});
</script>