How to decrease numbers in another form
This article will guide you on how to use a variable field in one form to decrease a number in another form. The example we will cover will include 2 forms, an Event Management Form – Admin Only and an Event Booking Form.
This is a variation to the existing Formidable Forms‘ Knowledge Base article Decrease an available count in another form.
Here’s the set-up:
Form 1: Event Management Form – Admin Only
This form is used for adding new events and contains the following fields (although you can add any fields you like):
- Event Name
- Event Date
- Number of places available
- Event Description
Form 2: Event Booking Form
In order to decrease a number in another form, you will need a second form.
This form is your event booking form and it uses a Dynamic Dropdown field to look at Form 1 > Event Date which will then display the dates available.
The form also uses a 2nd Dynamic List field to display the number of places still available and a hidden field to capture this number which can then be used elsewhere in the form to control conditional logic for your form elements if the number of places reaches the X limit.
Use the function below to make the number of seats decrease by a variable amount (instead of a fixed amount like the KB article shows):
add_action('frm_after_create_entry', 'after_entry_created', 30, 2);
function after_entry_created($entry_id, $form_id){
if($form_id == 9){ //change 9 to the ID of your reservations form
global $wpdb;
$reward_ids = $_POST['item_meta'][95]; //change 95 to the ID of your Dynamic dropdown field in your reservations form
$booking_num = $_POST['item_meta'][96]; //change 96 to the ID of your booking places field in your reservations form
$seat_count_field = 91; //change 91 to the ID of the available seats field in the event form
foreach ( (array) $reward_ids as $reward_id ) {
$available = FrmEntryMeta::get_entry_meta_by_field( $reward_id, $seat_count_field, $booking_num, true );
$wpdb->update( $wpdb->prefix .'frm_item_metas', array( 'meta_value' => ( (int) $available-$booking_num ) ), array( 'item_id' => $reward_id, 'field_id' => $seat_count_field ) );
}
}
}
We’ve referenced various Fields in the snippet above. For more information, see below:
95 is the ID of the Dynamic Dropdown field in your booking form.
96 is the ID of the number of places that are being booked.
91 is the ID of the number of places in Form 1
9 is the ID of Form 2
And Thats It!
Check out the other demos and tutorials in our blog using the links below.