• Resolved sitenerds

    (@sitenerds)


    Hi,

    I wanted to know the possibility of having a field having conditional logic based on a specific field in the Post Data form field.

    For example, I am building a Business Directory with a custom post type of “Listing”. The listings has a categories field with in, which can have the business category e.g. Restaurant, Plumber etc.

    https://ibb.co/6X39Qyd

    I want a Number field to be able to have conditional logic to show only if the business category is Restaurant. Is there any way to implement this? Currently it only gives me the option to put conditional logic based on the whole post (see screenshot)

    https://ibb.co/r3jQ6N3

    https://ibb.co/PwnWR1z

Viewing 7 replies - 16 through 22 (of 22 total)
  • Thread Starter sitenerds

    (@sitenerds)

    Here is the code I have implemented:

    <?php
    add_action(
    	'wp_head',
    	function() {
            global $post;
            if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
                return;
            }
            
            $form_id = 317;
            $cat_id = 50;
            $cond_field = 'number-2';
            
    		?>
    		<script type="text/javascript">
    			jQuery(document).bind("ready ajaxComplete", function(){
    				if(jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-category"]').val() == <?php echo $cat_id; ?>) {
    					jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
    				} else {
    					jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
    				}
    				jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-category"]').bind("select2:select", function(e){
    					if(jQuery(this).val() == <?php echo $cat_id; ?>) {
    						jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
    					} else {
    						jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
    					}
    				});
    			});
            </script>;
    
    		<?php
    	}
    );

    And here is the URL: https://staging2.alih33.sg-host.com/register-form/

    The expected conditional business category is meant to be “Baking”

    Plugin Support Laura – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @sitenerds,

    Thanks for share the link, could you please try this new code?

    <?php
    add_action(
    	'wp_head',
    	function() {
            global $post;
            if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
                return;
            }
            
            $form_id = 317;
            $cat_id = 50;
            $cond_field = 'number-2';
            
    		?>
    		<script type="text/javascript">
    			jQuery(document).bind("ready ajaxComplete", function(){
    				if(jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-hp_listing_category"]').val() == <?php echo $cat_id; ?>) {
    					jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
    				} else {
    					jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
    				}
    				jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-hp_listing_category"]').bind("select2:select", function(e){
    					if(jQuery(this).val() == <?php echo $cat_id; ?>) {
    						jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
    					} else {
    						jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
    					}
    				});
    			});
            </script>;
    
    		<?php
    	}
    );

    Let us know the results.

    Best regards,
    Laura

    Thread Starter sitenerds

    (@sitenerds)

    Hello,

    Perfect, that worked! Looks like the category needed to be that of the specific custom post type and not WP posts.

    Thanks so much for the team’s perseverance on this. Really appreciated.

    Thread Starter sitenerds

    (@sitenerds)

    Hi again,

    Sorry one more question, if I wanted to add multiple category IDs, how could I add that into the code?

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @sitenerds,

    I hope you are doing well today!

    To modify the code to support multiple category IDs, you can use an array of category IDs and then check if the selected category ID is in that array.

    <?php
    add_action(
        'wp_head',
        function() {
            global $post;
            if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
                return;
            }
            
            $form_id = 317;
            // Use an array for multiple category IDs
            $cat_ids = [50, 51, 52]; // Example: Adding categories 50, 51, and 52
            $cond_field = 'number-2';
            
            ?>
            <script type="text/javascript">
                jQuery(document).bind("ready ajaxComplete", function(){
                    // Convert PHP array to JavaScript array
                    var catIds = <?php echo json_encode($cat_ids); ?>;
                    if(catIds.includes(parseInt(jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-hp_listing_category"]').val()))) {
                        jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
                    } else {
                        jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
                    }
                    jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-hp_listing_category"]').bind("select2:select", function(e){
                        if(catIds.includes(parseInt(jQuery(this).val()))) {
                            jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
                        } else {
                            jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
                        }
                    });
                });
            </script>
            <?php
        }
    );
    • The $cat_id variable is replaced with $cat_ids, which is an array of category IDs ([50, 51, 52]).
    • In the JavaScript part, we have used var catIds = <?php echo json_encode($cat_ids); ?>; to pass the PHP array to JavaScript. This creates a JavaScript array with the same values.
    • To check if the selected category is in the array, we have used catIds.includes(parseInt(jQuery(this).val())). This checks if the value from the select field (converted to an integer for safe comparison) is in the catIds array.
    • The rest of the code remains largely the same, adjusting only for the fact that you’re now checking against multiple values instead of just one.

    This approach allows you to easily add or remove category IDs from the $cat_ids array without having to significantly alter your JavaScript logic.

    Kind regards,
    Zafer

    Thread Starter sitenerds

    (@sitenerds)

    Hi,

    Thanks for this, it seems to have worked. However, it has created a new issue where the post data field is not posting the data to the post type.

    The submission is showing in the backend in the submissions area of forminator but not being posted.

    I tried removing the snippet but it seems to have caused a permanent effect as it doesn’t work even after removing it. However, when I try on a staging instance that I have never used the snippet on, it works.

    Can you advise?

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @sitenerds,

    I tested the form and the snippet on my test site and I’m not able to replicate any such issue the form submission works fine and it does show up as pending posts. What happens if a new form with only the post data is created? Does that work?

    Could you please check and see whether you could replicate the same issue if a new form is created in the staging site and with the snippet added?

    Did you also try creating a test post directory via backend? Does it throw any errors?

    Please do let us know how the above goes.

    Kind Regards,

    Nithin

Viewing 7 replies - 16 through 22 (of 22 total)
  • The topic ‘Conditional Logic based on post data field’ is closed to new replies.