Custom Button javascript not working

karen

Member
Hi, I have successfully placed a custom button at the top of the details page, using the template in the documentation.
I want the button to be an "order now" button, so that when clicked, the details of the current record are sent to sales via email. I want a message to pop up saying "order sent successfully" when clicked. I have made the custom javascript, but when I click the button, nothing happens, and no email is received.

My custom buttons code is:
$custom_buttons['Hoses'][$cnt]['type'] = 'javascript';
$custom_buttons['Hoses'][$cnt]['callback_function'] = 'dadabik_send_order';
$custom_buttons['Hoses'][$cnt]['permission_needed'] = 'read';
$custom_buttons['Hoses'][$cnt]['show_in'][] = 'details_page';
$custom_buttons['Hoses'][$cnt]['position_form'] = 'top';
$custom_buttons['Hoses'][$cnt]['label_type'] = 'fixed';
$custom_buttons['Hoses'][$cnt]['label'] = 'CLICK HERE TO ORDER';
$custom_buttons['Hoses'][$cnt]['style'] = 'background:#c76128;width:200px';
$custom_buttons['Hoses'][$cnt]['id'] = 'order_now_button';
$cnt++;

My javascript code is (in custom_functions.js):

function dadabik_send_order()
{

// Check if the button is clicked
if (document.getElementById('cb__order_now_button').clicked) {
// Get the current logged in user name
var current_user = sessionStorage.getItem('logged_user_infos_ar')['id_user'];

// Get the fields from the current displayed record
var field1 = document.getElementById('hose_id').value;
var field2 = document.getElementById('site').value;
// Add more fields as needed

// Compose the email subject and body
var subject = "New order from portal";
var body = "From: " + current_user + "\n\n";
body += "Field 1: " + field1 + "\n";
body += "Field 2: " + field2 + "\n";
// Add more fields as needed

// Set the email recipient
var to = "sales@xxxxx.co.uk";

// Set the email headers
var headers = "From: " + current_user + "\r\n";
headers += "Reply-To: " + current_user + "\r\n";

// Send the email
if (sendEmail(to, subject, body, headers)) {
// Display a success message
alert('Email successfully sent');
} else {
// Display an error message
alert('Failed to send email');
}
}

Any advice gratefully received, hopefully someone can spot something very obvious I have done wrong!
Thanks in advance.
 
Top