Is there a way to add conditional logic to a form using L&L

Hello,
I just found the L&L plugin yesterday and find it very intuitive. I am wondering if there is a way to use the if/else logic provided by L&L in a form. I am looking for an easy way to trigger different actions depending on the input into a form field (e.g. a number), once the submit button is clicked.
I will appreciate your feedback!

L&L won’t be able to interact directly with your form in the current version of the plugin, but we’ve got some powerful stuff in the pipeline that might help with that use case down the road.

For now, most form plugins allow you to set up a form so that when a user submits an entry, it sends them to a particular URL and appends URL query parameters onto the URL, like https://example.com?color=blue&size=small. If you’ve got an L&L template on that form confirmation page, then you could use the Url tag to do some fun conditional logic based on the values. Could look something like this:

<If check="{Url query=color}" value=blue>
  The value is blue!
  <Else />
  The value is not blue :(
</If>

Hope that helps!

1 Like

Formidable Forms can do this using conditional logic under the form actions settings.

Probably Gravity Forms as well which will have better integration with L&L soon apparently.

The big forms plugins all come with a significant cost and time investment though. :slight_smile:

1 Like

Thank you for the feedback!

Unfortunately, Formidable Forms comes at a considerable cost for a hobbyist.

I have tried Benjamin’s approach and it is working :grin:

However, now I find the Redirect tag not working for me. :confused: I have used this code:

Yes, he has a dog! Yes, he has a dog!

If the value is neither “Hund” nor “hund” (German for dog), e.g.“Katze” (German for cat), I am not redirected anywhere. If I put in a text instead, it is working as expected. I think that I am missing something very basic here.

I will highly appreciate any hint!

The code didn’t make it with my previous message. Sorry for that.

<If check="{Url query=antwort8}" value=Hund>
    Yes, he has a dog!
    <Else if check="{Url query=antwort8}" value=hund />
    Yes, he has a dog!
    <Else />
    <Redirect to="https://google.com" />
  </If>type or paste code here

It looks like you are missing the 'is' comparison value, and you can use the <Switch> tag to simplify this logic. <Switch> is better when you are using the same comparison value repeatedly and may add to the logic in future. :slight_smile:

This logic should work:

<Switch check="{Url query=antwort8}" is>
    <When value="Hund" />
    <p>Yes, he has a dog!</p>
    <When value="hund" />
    <p>Yes, he has a dog!</p>
    <When />
    <Redirect to="https://google.com" />
</Switch>

Switch and When tags:

Comparison options:

Thank you for providing a better code. I really appreciate it!

Unfortunately, the logic is not the problem here. If I remove the redirect tag and add a text statement instead (see below), everything works out as expected.

<Switch check="{Url query=antwort8}" is>
    <When value="Hund" />
    <p>Yes, he has a dog!</p>
    <When value="hund" />
    <p>Yes, he has a dog!</p>
    <When />
    <p>He does not have a dog!</p>
</Switch>

As soon as I add the redirect tag, I am stuck. I have already deactivated all plugins except for elementor and tangible and also tried with a different theme (wp twenty nineteen instead of hello elementor) without success. The redirect tag also doesn’t work if I use it within L&L in a different context.

I am using wp version 6.3.1-DE and php version 7.4.33.

I found a work around with the html meta tag. It is not perfect because the page of the first part of the logic statement is loaded and displayed briefly before the redirect takes place.

<Switch check="{Url query=antwort8}" is>
  <When value="Hund" />
    <p>Yes, he has a dog!</p>
  <When value="hund" />
    <p>Yes, he has a dog!</p>
  <When />
    <meta http-equiv="refresh" content="0; URL=https://www.google.com" />
</Switch>

Any better ideas or suggesetions?

The <Redirect> tag works fine for me, so this must be an issue with your setup, perhaps there is a conflict with a security policy on your server. :slight_smile:

1 Like

Redirects seem to be working as expected on my end too, so I’m not sure what’s going on here. Let us know if you’re able to look into your server setup and see if a setting there might be causing your issue.

I just wanted to chime in to suggest a possible improvement for your template. Instead of using the comparison “is” you could use one of the other comparisons that would allow you to cut down on your repetition. Something like this:

<If check="{Url query=antwort8}" in value="Hund,hund">
    Yes, he has a dog!
    <Else />
    He does not have a dog!
  </If>

Keep us posted about your issue with the redirect.

Thank you so much for your input. Checking the error log of the FTP server, I found two PHP warnings stating that the header information cannot be modified (see below). As I am not an IT person, I have no clue how to fix it. It seems not to be related to the theme that I use as the Redirect tag also didn’t work after switching to a default wordpress theme.

[Tue Oct 03 19:12:14.353132 2023] [fcgid:warn] [pid 4133684] [client 92.104.182.188:0] mod_fcgid: stderr: PHP Warning: Cannot modify header information - headers already sent by (output started at /home/ch359825/web/basel-ermittelt.ch/public_html/wp-includes/formatting.php:5856) in /home/ch359825/web/basel-ermittelt.ch/public_html/wp-includes/pluggable.php on line 1435, referer: testtask1 – BASEL ERMITTELT
[Tue Oct 03 19:12:14.353163 2023] [fcgid:warn] [pid 4133684] [client 92.104.182.188:0] mod_fcgid: stderr: PHP Warning: Cannot modify header information - headers already sent by (output started at /home/ch359825/web/basel-ermittelt.ch/public_html/wp-includes/formatting.php:5856) in /home/ch359825/web/basel-ermittelt.ch/public_html/wp-includes/pluggable.php on line 1438, referer: testtask1 – BASEL ERMITTELT

Any help is highly appreciated!