Count Number of Records
- Details
- Category: Blog
- Published: Thursday, 04 July 2019 10:46
- Written by Mario Puco
To count the number of records for a certain form and insert a record count in a field in a form, the best way is to create a custom submit button which has in an Actionscript an Ajax call to the database to get the count. That way, records will be counted when the submit button is pressed.
To do so, please follow these steps:
1) Go to your form's Properties and set "Include submit button" to "No". That will remove regular submit button.
2) At the end of your form create new element of Submit button type. What you write in the "Value" field for that button element is what will be displayed on that button in your form, so that it the place where you should put, for example, "SUBMIT" since this button will use to submit the form.
3) Go to the "Advanced" properties of that newly created submit button element and in the "Actionscript" section set "Type" to "Custom" and mark "Click" as "Action".
In the code are below put the following code:
function ff_submit_action(element, action) { jQuery.ajax({ type: 'GET', url: 'http://localhost/value.php', data: 'test', success: function(data){ ff_getElementByName("ELEMENTNAME").value = data; if(typeof bfUseErrorAlerts != 'undefined'){ JQuery(".bfErrorMessage").html(''); JQuery(".bfErrorMessage").css("display","none"); } error = ff_validation(0); if (error != '') { if(typeof bfUseErrorAlerts == 'undefined'){ swal(error); } else { bfShowErrors(error); } ff_validationFocus(); } else ff_submitForm(); } }); } // ff_submit_action
NOTE: In the code above you have to replace "submit" with the name of the submit button that you have created.
Furthermore, replace ELEMENTNAME with the name of the name of element in your form which should be filled with count number.
Finally, make sure to replace 'http://localhost/value.php' with the correct path for your site to the "value.php" file.
4) Go to the Joomla root folder and create there file named value.php with the following content:
<?php define( '_JEXEC', 1 ); define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' )); require_once ( JPATH_BASE .'/includes/defines.php' ); require_once ( JPATH_BASE .'/includes/framework.php' ); $db = JFactory::getDBO(); $db->setQuery('Select Count(*) From #__facileforms_records Where name="MyFormName"'); $result = $db->loadResult()+1; echo json_encode($result); ?>
NOTE: Make sure to replace MyFormName with the actual name of your form.
That will fill the form field with the record count.
Solution provided by MihaelaK ( thanks ).