Distilled from this forum thread.

Introduction

Forms in FrontAccounting often have values of certain fields determining the layout, visibility and value of other field(s) besides acting as submit buttons for transparent data updation in the backend. This is achieved through Ajax.

Backend Files

includes/ajax.inc
includes/JsHttpRequest.php
js/JsHttpRequest.js

Examples

  • In the Setup => Install/Update Languages page in the default company, by clicking the checkbox at the top viz., Display also languages not supported by server locales we get an instanlty refreshed view of all possible language files available and equally instantly does it revert back on unchecking it.
  • In Sales => Direct Invoice page we choose an item from the dropdown box and automatically the Price before Tax is filled in for us from the default price list. We are free to alter the price in the said field at will. Foreign Items Invoicing autofill ajax workaround are in this forum post.
cart_id=54762f4921147
customer_id=1
branch_id=1
ref=1
payment=4
sales_type=1
OrderDate=11/26/2014
dimension_id=0
dimension2_id=0
_stock_id_edit=ABC_123
stock_id=ABC_123
_stock_id_update=%20
qty=1
price=0.00
Disc=0.00
freight_cost=0.00
Location=DEF
Comments=
delivery_date=11/26/2014
_focus=customer_id
_modified=0
_token=e73635747d6afe9c9fc1843319640b855ae371731521d10dcbe0dc9ea43fdd8d
_random=162315.9726003423

Inner Workings

  • All functions that assist ajax responses are in includes\ui\ui_lists.inc.

If on the change of value of any FA form element, some ajax work needs to be done (like other form elements getting updated or some function being executed), then a $_POST array element needs to be set (=1) and sent forthwith along with all current form data without waiting for form submission.

The $_POST array element must bear a name that embeds the ajax requesting element's name attribute as seen in lines 300 to 307 in the function list_updated($name) in includes/ui/ui_lists.inc file:

/*
	Helper function.
	Returns true if selector $name is subject to update.
*/
function list_updated($name)
{
	return isset($_POST['_'.$name.'_update']) || isset($_POST['_'.$name.'_button']);
}

Hence if the ajax requesting form element is stock_id then the hidden form element that is sent over for an ajax call in FA would either be: $_POST['_stock_id_update'] or $_POST['_stock_id_button'] for the function list_updated($name) to trigger the ajax response.

To get the response inside a target form element, it's name attribute is passed on like $Ajax->activate('price').

This is seen for example in lines 506-511 in sales/includes/ui/sales_order_ui.inc:

		if (list_updated('stock_id')) {
			    $Ajax->activate('price');
			    $Ajax->activate('units');
			    $Ajax->activate('qty');
			    $Ajax->activate('line_total');
		}

Ajax timeouts

  • Forum Post: Print Recurring Invoices type of pages workaround is discussed.

To use the long Ajax timeout (and display the horizontal time icon like in backup) change:

        submit_center_first('confirmed'.$id, _('Create'), _('Create recurrent invoices'), false, ICON_OK);

to:

        submit_center_first('confirmed'.$id, _('Create'), _('Create recurrent invoices'), "process", ICON_OK);