Select Page

Refresh the page after updating a field value in Formidable forms

Formidable Forms provides an easy way to update a field value with a click on a link. However one of its shortcomings is, if the value being updated by the link is visible on the page, it is not updated to its new value as shown below.

In this post, we are going to fix it by introducing a custom shortcode which utilizes existing Formidable code where possible and enhance it where it is required.

Step 1:
Create a new shortcode frm_city_entry_update_field which replaces the Formidable Javascript function frmUpdateField in the link with a modified function frmCityUpdateField. Add this to functions.php or use Code Snippets plugins which is our preferred method for adding custom scripts.

add_shortcode('frm-city-entry-update-field', 'frm_city_entry_update_field');
function frm_city_entry_update_field( $atts ) {
	//Call Formidable forms function which is used by original shortcode
	$link = FrmProEntriesController::entry_update_field($atts);
	
	//Inject enhanced version of Javascript code which supports page refresh and confirmation message options.
	if(!empty($link)) {
		preg_match('/(?<=frmUpdateField)\([^\)]*\)/', $link, $matches);
		if($atts['confirmation_msg']){
			$confirm_script = "onclick=\"if(confirm('" . $atts['confirmation_msg'] . "')){frmCityUpdateField" . $matches[0] . ";}return false;\"";
		} else {
			$confirm_script = "onclick=\"frmCityUpdateField" . $matches[0] . "; return false;\"";			
		}
		$link = preg_replace('/onclick=\"[^\"]*\"/', $confirm_script, $link);
	}
	return $link;
}

Step 2:
frmCityUpdateField Javascript function is identical to frmUpdateField function except it refreshes the page upon successfully updating the field. Here we used Code Snippet plugin to inject Javascript code. If you use a different approach, you may have to copy only the Javascript part which starts with <script> and end with </script>

add_action( 'wp_head', function () { ?>
	<script>
		function frmCityUpdateField(entry_id, field_id, value, message, num) {
			jQuery(document.getElementById("frm_update_field_" + entry_id + "_" + field_id + "_" + num)).html('<span class="frm-loading-img"></span>');
			jQuery.ajax({
				type: "POST",
				url: frm_js.ajax_url,
				data: {
					action: "frm_entries_update_field_ajax",
					entry_id: entry_id,
					field_id: field_id,
					value: value,
					nonce: frm_js.nonce
				},
				success: function() {
					if (message.replace(/^\s+|\s+$/g, "") === "")
						jQuery(document.getElementById("frm_update_field_" + entry_id + "_" + field_id + "_" + num)).fadeOut("slow");
					else
						jQuery(document.getElementById("frm_update_field_" + entry_id + "_" + field_id + "_" + num)).replaceWith(message);

					location.reload();
				}
			})
		}
	</script>
<?php } );

Step 3:
Use the new shortcode frm-city-entry-update-field in place of frm-entry-update-field shortcode. All attributes of the shortcode will work as usual.

e.g.
[frm-city-entry-update-field id=[id] field_id=205 value="Open" label="Open this venue"]

Bonus:
As an added bonus, frm-city-entry-update-field provides an optional confirmation_msg attribute to which prompts the user to confirm before updating the field value.

e.g.

[frm-city-entry-update-field id=[id] field_id=205 value="Open" label="Open this venue" confirmation_msg="Are you sure want to Open this Venue?"]


Random number in a Formidable forms field

Sometimes we want to set a random number in a form field. It is easy to do it in Formidable forms with the help of the “Default value” calculation.

In the “Default value” option, select calculator and set the value to Math.floor(Math.random() * 100) + 1 to generate a random number between 1 and 100.

Use the following formula to generate a random number in any value range by replacing min and max values.
Math.floor(Math.random() * (max - min + 1)) + min

Here is a working example

Random number in a field

Formidable URL field Cheat sheet

Learn all about Formidable Forms URL field in this Cheat sheet.

Field Type – URL
This URL field requires a value. If no value provided, there will be a validation error message (customizable). The default “required field indicator” is * (customizable).
This is a read-only URL field. User can’t choose any options here. However, options can be set indirectly using “Default Value” in field settings or programmatically.
Use [siteurl] shortcode to get the URL of the site.
Use [siteurl][server param=”REQUEST_URI”] shortcodes to get current page
Use [server param=”HTTP_REFERER”] shortcode to get referrer paege.

Formidable email field Cheat sheet

Learn all about Formidable Forms email field in this Cheat sheet.

Field Type – Email
This email field requires a value. If no value provided, there will be a validation error message (customizable). The default “required field indicator” is * (customizable).
This is a read-only checkbox. User can’t choose any options here. However, options can be set indirectly using “Default Value” in field settings or programmatically.
When emails are not matched, there will be an error message displayed (customizable)
An email address can be specified in the “Default Value” settings. In order to display logged in user’s email address, use [email] shortcode
@example.com
This is not a Formidable email field. Instead, it is textbox field with the domain name set as an appended value (requires “Bootstrap Forms add-on”).

Formidable date field Cheat sheet

Learn all about Formidable Forms date field in this Cheat sheet.

Field Type – Date
This date field requires a value. If no value provided, there will be a validation error message (customizable). The default “required field indicator” is * (customizable).
This is a read-only date field. User can’t choose any options here. However, options can be set indirectly using “Default Value” in field settings or programmatically.
Here date field’s default value is set to a specific date (2020-01-01)
The current date can be set with [date] shortcode. An offset can be used to set a date relative to current date.
e.g. [date offset=’+2 weeks’]
e.g. [date offset=’-1 month’]
e.g. [date offset=’+1 year 3 weeks’]
e.g. [date offset=’Next Monday’]
e.g. [date offset=’Last Friday’]
e.g. [date offset=’First day of this month’]
e.g. [date offset=’Last day of next month’]
This date field only allows selecting Mon, Tue, and Wed. This requires the “Datepicker Options” add-on.
This date field doesn’t allow to select “2019-09-24” and “2019-09-25”. This requires the “Datepicker Options” add-on.
This date field allows selecting a date in the current year and next 3 years.
This date field allows a date between today and 30 days from today. This requires the “Datepicker Options” add-on.
There are multiple ways of setting a min and a max of the date range allowed. They are
  • A specific date. e.g. 2020-01-01. As of writing this, this can’t be dynamically set using a shortcode such as [date offset=’+2 weeks’]
  • Current date with an optional offset. Offset only supports +/- n days/weeks/months/years format. Other formats like “Next Monday”, “First day of this month” are not supported as of writing this.
  • A date from another date field with an optional offset (Refer the example below). Offset only supports +/- n days/weeks/months/years format. Other formats like “Next Monday”, “First day of this month” are not supported as of writing this.
Following example shows how a date field’s date range can be controlled from another date field.

The “travel start date” allows selecting today or a future date. The “travel end date” allows selecting a date from the next day of travel start date until a month from the travel start date.
This requires custom coding with frm_date_field_options hook.