How to add Button with Mail-Function to Detail-Page

mrisse

New member
Hi,

I want to add a custom button on detail page to send an auto-generated email to the customer. I don't know how to start without creating a complete new template. Some tipps or examples are welcome ;-)

Best,
Marc
 

eugenio

Administrator
Staff member
Hello Marc,
you don't need to create a template, you just need to declare the button ($custom_buttons, see the documentation) and write the few lines of code needed to retrieve the info you need and to send them via email.

Best,
 

mrisse

New member
Hi,

i managed to add a custom php function with a button on detail page. works well.
my function sends huge mails and it takes about 20 seconds to finish. so i tried to change button from php_standard to php_ajax to show a progress indicator. my function works, but after it is finished it shows an alertbox with:

[pre]
unexpected error custom function ajax call: {"readyState":4,"responseText":"<!DOCTYPE html>\n\n\n<html\n\n>\n<head>\n<title>Cakery</title>\n<link rel=\"stylesheet\" href=\"css/normalize.css\" type=\"text/css\" media=\"screen\">\n<link rel=\"stylesheet\" href=\"css/styles_screen.css\" type=\"text/css\">\n\n\n\n\n<meta charset=\"utf-8\">\n<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n<meta name=\"Generator\" content=\"DaDaBIK 9.2.1 Monterosso - http://dadabik.com/\">\n<meta name=\"viewport\" content=\"initial-scale=1.0\"/>\n<script language=\"javascript\" type=\"text/javascript\" src=\"include/tinymce/tinymce.min.js\"></script>\n \n<script src=\"include/jquery/jquery-1.9.1.min.js\"></script>\n\n<script src=\"include/jquery/jquery-ui-1.12.1/jquery-ui.min.js\"></script>\n<link rel=\"stylesheet\" href=\"include/jquery/jquery-ui-1.12.1/jquery-ui.min.css\" type=\"text/css\" media=\"screen\">\n\n<script src=\"include/date_picker/jquery-ui-timepicker-addon.js\"></script>\n<link rel=\"stylesheet\" href=\"css/jquery-ui-timepicker-addon.css\" type=\"text/css\" media=\"screen\">\n\n\n<link rel=\"stylesheet\" href=\"css/fontawesome.css\">\n<link rel=\"stylesheet\" href=\"css/bootstrap.css\">\n\n\n\n\n\n<link href=\"css/select2/select2.min.css\" rel=\"stylesheet\" />\n<script src=\"include/select2/select2.min.js\"></script>\n \n\n\n\n<link rel=\"stylesheet\" href=\"css/styles_screen_custom.css\" type=\"text/css\" media=\"screen\">\n\n<script language=\"javascript\" type=\"text/javascript\">\ntinyMCE.init({\n\tmode : \"specific_textareas\",\n\teditor_selector : \"rich_editor\",\n\tbranding: false,\n\tplugins: [\n\t\t\"advlist autolink lists link image charmap print preview anchor\",\n\t\t\"searchreplace visualblocks code fullscreen\",\n\t\t\"insertdatetime media table contextmenu paste\"\n\t]\n});\n</script>\n\n\n\n<script src=\"include/custom_functions.js\"></script>\n\n\n<script>\nfunction uncheck_other_homepage_checkboxes(index){\n\n\tif ( $(\"#is_homepage_static_page_\"+index).is(':checked') === true ){\n\t\tfor (i=1;i<=Number($(\"#number_items\").val());i++){\n\t\t\tif ( i != index){\n\t\t\t\t$(\"#is_homepage_static_page_\"+i).prop('checked', false);\n\t\t\t}\n\t\t}\n\t}\n}\nfunction enable_disable_loader(action)\n{\n\tif (action === 'enable'){\n\t\t $(\"body\").addClass(\"loading\"); \n\t}\n\telse if(action === 'disable'){\n\t\t $(\"body\").removeClass(\"loading\"); \n\t}\n}\n\nfunction remove_showing_confirmation_message_class()\n{\n\t $(\"body\").removeClass(\"showing_confirmation_message\");\n}\n\n\n$(document).ready(function() {\n\n\n$(function () {\n\t$('li.has-sub-menu > a').on(\"click\", function (e) {\n\t\te.preventDefault();\n\t\tvar $listItem = $(this).parents(\"li:first\");\n\t\t$listItem.toggleClass(\"sub-menu-opened\");\n\t\t$listItem.toggleClass(\"selected\");\n\t});\n\n\t$('[data-mobile-menu-toggle]').on(\"click\", function () {\n\t\t$('.main-menu-container').toggleClass(\"menu-mobile-opened\");\n\t});\n});\n\t\t\n \n//$(\"#menu\").eosMenu();\n\n//$( \".eos-item-selected\" ).css('display', 'none');\n\n\t\n\t$('#confirmation_message_container').on('click', '#error_message_close_link', function (e) {\n\t\t$(\"#error_message\").css('display', 'none');\n\t\te.preventDefault()\n\t});\n\t$('#confirmation_message_container').on('click', '#confirmation_message_close_link', function (e) {\n\t\t$(\"#confirmation_message\").css('display', 'none');\n\t\te.preventDefault()\n\t});\n\t$('#confirmation_message_container').on('click', '#alert_message_close_link', function (e) {\n\t\t$(\"#alert_message\").css('display', 'none');\n\t\te.preventDefault()\n\t});\n\t\n\t// adjust diabled attribute for template_table when the page is loaded\n\tif ($('#enable_template_table_checkbox').is(':checked')){\n\t\t$('#template_table').prop('disabled', false);\n\t}\n\telse{\n\t\t$('#template_table').prop('disabled', true);\n\t\t\n\t}\n\t\n\t// adjust it again when the user click on the enable checkbox\n\t$(\"#enable_template_table_checkbox\").click(function(e) {\n\t\n\t\tif ($('#enable_template_table_checkbox').is(':checked')){\n\t\t\t$('#template_table').prop('disabled', false);\n\t\t}\n\t\telse{\n\t\t\t$('#template_table').prop('disabled', true);\n\t\t\t\n\t\t}\n\t});\n\t\n\t/*$(\"#date_function\").on('change', function() {\n\t\talert(0);\n\t\tshow_hide_date_functions_select();\n\t});*/\n\t\n\t\n\t$(\"#craete_new_view_link\").click(function(e) {\n\t\t\n\t\tif ($(\"#create_new_view_form\").css('display') === 'block'){\n\t\t\t$(\"#create_new_view_form\").css('display', 'none');\n\t\t}\n\t\telse{\n\t\t\t$(\"#create_new_view_form\").css('display', 'block');\n\n\t\t}\n\t\te.preventDefault();\n\t});\n\t\n\n\t$(\"#include_tables_form\").submit(function(e) {\n\t\n\t\t\n\t\tvar field_names_to_update = [];\n\t\tvar field_values_to_update = [];\n\t\tvar where_values = [];\n\t\t\n\t\tenable_disable_loader('enable');\n\t\t\n\t\tvar number_items = $('#number_items').val();\n\t\t\n\t\tvar pages_to_show = $('#pages_to_show').val()\n\t\t\n\t\tvar error = '';\n\t\t\n\t\tif (pages_to_show === 'custom_pages'){\n\t\t\n\t\t\tvar tablename_to_update = 'dadabik_static_pages';\n\t\t\t\n\t\t\tfor (i=0;i<number_items;i++){\n\t\t\t\tfield_names_to_update = [];\n\t\t\t\tfield_values_to_update = [];\n\t\t\t\tfield_names_to_update[0] = 'link_static_page';\n\t\t\t\tfield_values_to_update[0] = $('#link_static_page_'+(i+1)).val();\n\t\t\t\t\n\t\t\t\tfield_names_to_update[1] = 'enabled_static_page';\n\t\t\t\tif ($('#enabled_static_page_'+(i+1)).is(':checked')){\n\t\t\t\t\tfield_values_to_update[1] = '1';\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tfield_values_to_update[1] = '0';\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tfield_names_to_update[2] = 'file_static_page';\n\t\t\t\tfield_values_to_update[2] = $('#file_static_page_'+(i+1)).val();\n\t\t\t\n\t\t\t\tfield_names_to_update[3] = 'content_static_page';\n\t\t\t\tfield_values_to_update[3] = tinyMCE.get('content_static_page_'+(i+1)).getContent();\n\t\t\t\t\t\t\t\n\t\t\t\tfield_names_to_update[4] = 'menu_parent_page';\n\t\t\t\tfield_values_to_update[4] = $('#menu_parent_page_'+(i+1)).val();\n\t\t\t\n\t\t\t\tfield_names_to_update[5] = 'is_homepage_static_page';\n\t\t\t\n\t\t\t\tif ($('#is_homepage_static_page_'+(i+1)).is(':checked')){\n\t\t\t\t\tfield_values_to_update[5] = 'y';\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tfield_values_to_update[5] = 'n';\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tfield_names_to_update[6] = 'type_static_page';\n\t\t\t\tfield_values_to_update[6] = $('#type_static_page_'+(i+1)).val();\n\t\t\t\t\t\t\t\n\t\t\t\tfield_names_to_update[7] = 'menu_order_static_page';\n\t\t\t\tfield_values_to_update[7] = $('#menu_order_static_page_'+(i+1)).val();\n\t\t\t\t\t\t\t\n\t\t\t\tfield_names_to_update[8] = 'href_link_static_page';\n\t\t\t\tfield_values_to_update[8] = $('#href_link_static_page_'+(i+1)).val();\n\t\t\t\t\n\t\t\t\tfield_names_to_update[9] = 'separator_before_static_page';\n\t\t\t\tif ($('#separator_before_static_page_'+(i+1)).is(':checked')){\n\t\t\t\t\tfield_values_to_update[9] = 'y';\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tfield_values_to_update[9] = 'n';\n\t\t\t\t}\n\n\n\t\t\t\t\n\t\t\t\tif (field_values_to_update[6] === 'PHP' && field_values_to_update[2] === '') {\n\t\t\t\t\terror = 'If you choose PHP as <i>type</i>, you need to specify a PHP source file as well.';\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (field_values_to_update[6] === 'html' && field_values_to_update[3] === '') {\n\t\t\t\t\terror = 'If you choose HTML as <i>type</i>, you need to specify the HTML content as well.';\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (field_values_to_update[6] === 'link' && field_values_to_update[8] === '') {\n\t\t\t\t\terror = 'If you choose link as <i>type</i>, you need to specify the href link as well.';\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t\n\n\t\t\t\t\n\t\t\t\t\n\t\t\t\t\n\t\t\t\twhere_values = $('#id_static_page_'+(i+1)).val();\n\t\t\t}\n\t\t}\n\t\telse{\n\t\t\tvar tablename_to_update = 'dadabik_table_list';\n\n\t\t\tfor (i=0;i<number_items;i++){\n\t\t\t\tfield_names_to_update = [];\n\t\t\t\tfield_values_to_update = [];\n\t\t\t\tfield_names_to_update[0] = 'alias_table';\n\t\t\t\tfield_values_to_update[0] = $('#alias_table_'+(i+1)).val();\n\t\t\t\n\t\t\t\tfield_names_to_update[1] = 'pk_field_table';\n\t\t\t\tfield_values_to_update[1] = $('#pk_field_table_'+(i+1)).val();\n\t\t\t\n\t\t\t\tfield_names_to_update[2] = 'menu_parent_table';\n\t\t\t\tfield_values_to_update[2] = $('#menu_parent_table_'+(i+1)).val();\n\t\t\t\n\t\t\t\tfield_names_to_update[3] = 'allowed_table';\n\t\t\t\n\t\t\t\tif ($('#allowed_table_'+(i+1)).is(':checked')){\n\t\t\t\t\tfield_values_to_update[3] = '1';\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tfield_values_to_update[3] = '0';\n\t\t\t\t}\n\t\t\t\n\t\t\t\tfield_names_to_update[4] = 'menu_order_table';\n\t\t\t\tfield_values_to_update[4] = $('#menu_order_table_'+(i+1)).val();\n\t\t\t\t\n\t\t\t\tfield_names_to_update[5] = 'separator_before_table';\n\t\t\t\tif ($('#separator_before_table_'+(i+1)).is(':checked')){\n\t\t\t\t\tfield_values_to_update[5] = 'y';\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tfield_values_to_update[5] = 'n';\n\t\t\t\t}\n\t\t\t\n\t\t\t\twhere_values = $('
[/pre]
 

eugenio

Administrator
Staff member
Interesting use of the custom button!
I think the problem is here (I paste from documentation):

Please note that PHP callback functions normally are not used to produce output; php_ajax functions, in particular, rise errors if you produce output (via echo statements or in any other way).

You should just send the email with your function, you shouldn't produce any output (according to the debug statement I see, it seems you are producing output).

Best,
 

mrisse

New member
Hi,

i removed every output and debugging is disabled. I only use the header-call after mail:

[pre]
mail = @mail($to, $subject, $message, $headers, $returnpath);
header('Location:'.$dadabik_main_file.'?function=search&tablename='.urlencode($table_name).'&'.$where_field);
[/pre]
Is there an other way to return to the table?

Best,
Marc
 

eugenio

Administrator
Staff member
Hello,
the header call redirect to a page, which produces output. Can you confirm that without the header call it works?

You could just let the user go back to the results grid by using the back button, is there any reason why you want to force the redirect after having sent the email? The redirect URL also seems wrong.

Best,
 
Top