Use IF and ENDIF Variables to Control Content – WS Form

if … endif VariablesPRO

The #if and #endif variables are used to conditionally include content. They can be used to show or hide content in a variety of locations such as the ‘Send Email‘ action, ‘Show Message‘ action, conditional logic (e.g. when setting a field value or message content) or even in field mappings to external API’s.

Demo

Syntax

The basic syntax of an #if(...) statement is shown below.

#if(expression)
content
#endif

The expression is evaluated to its boolean value. If the expression evaluates to true, WS Form will output the content, and if it evaluates to false, WS Form will ignore it.

The parts of the expression must be separated by spaces.

An example #if(...) statement is shown below:

#if(#field(123))
<label>First Name</label>
<span>#field(123)</span>
#endif

In this example, the WS Form checks to see if field ID 123 contains any data. If it does, the label and span are output. If the field is blank, the label and span are not output.

Operators

Operators can be included in #if(...) statements to compare one value with another. The available operators are:

Operator Description Example
None Is Not Blank #if(#field(123))Show this#endif
== Equals #if(#field(123) == "this value")Show this#endif
!= Does Not Equal #if(#field(123) != "this value")Show this#endif
< Less Than #if(#field(123) < 456)Show this#endif
> Greater Than #if(#field(123) > 456)Show this#endif
<= Less Than Or Equal To #if(#field(123) <= 456)Show this#endif
>= Greater Than Or Equal To #if(#field(123) >= 456)Show this#endif
*= Contains #if(#field(123) *= "this value")Show this#endif
!*= Does Not Contains #if(#field(123) !*= "this value")Show this#endif
^= Starts With #if(#field(123) ^= "this value")Show this#endif
!^= Does Not Start With #if(#field(123) !^= "this value")Show this#endif
$= Ends With #if(#field(123) $= "this value")Show this#endif
!$= Does Not End With #if(#field(123) !$= "this value")Show this#endif

If you do not specify an operator, WS Form will check that the input is not blank. If it is blank, it will return false, if it is not blank, it will return true.

Multiple Conditions

You can use multiple conditions in an #if(...) statement, separated by && (AND) or || (OR) operators. For example:

#if(#field(1) == "Checkbox 1" && #field(2) == "Checkbox 2")Show this#endif

Compared Values

The values entered for comparison can be a string, e.g. “this value” or any valid WS Form variable, e.g. #field(123).

Using #if … #endif in Conditional Logic

#if(...) conditions can be used on your form as well as in action such sending emails or showing a message.

Conditional logic can be used reassess #if(...) conditions when the content of a compared value changes. An example of this conditional logic is shown below.

WS Form PRO - #if ... #endif - Conditional Logic

Examples

Conditional Styling

<span style="color:#if(#field(123) == "Green")#00ff00#endif#if(#field(123) == "Red")#ff0000#endif">Style this text</span>

Show Text Depending on Submission Status

#if(#submit_status == "draft")In Progress#endif
#if(#submit_status == "publish")Submitted#endif

Check Query Variable utm_source

#if(#query_var("utm_source") == "Twitter")Source is Twitter#endif
#if(#query_var("utm_source") == "Google Ads")Source is Google Ads#endif

Check Field Containing Age

#if(#field(123) < 21)Aged under 21#endif