Scripting - Server-Side Scripting

EZSurvey's compiled CGI scripts have an option called Print Report. This is based on our lightweight JavaScript interpreter that the CGI scripts also use to do validation, randomize responses, and more.

The ability to edit server-side scripting in the Edit Form Window is exclusive to EZS pro.

The way it works is that the report (or EZSurvey page) is standard HTML, with a few special markup tags. Any tag beginning with <% will be interpreted by the CGI program before being sent to the web browser. You should include a space before the close of the tag.

For instance, you can print a field name on the HTML page. It is good form for the tag to begin and end with a %.

Piping:

You can include the answer to a question on a previous page or a pre-filled hidden question as part of the HTML to be printed with following the syntax, this will give you the fieldname of the piped variable.

<P>Your name is <%=NAME %>
<P>Your age is <%=AGE %>

Calculations:

You can also do in-line calculations

<P>Your score is <%=Q2*4 + Q4*2 + Q5/10 %>

You can assign values to variables. Remember to put the semicolon after the statement. The question fieldname needs to be in brackets and it will return the field name of the response:

<% value1={Q1}; %>
Where value1 is any variable.

Environment Variables:

CGI Environment variables are obtained by using the $ character, for example:

<P>Your IP address <%=$REMOTE_HOST %>

Conditions:

To print different text under different conditions, use the syntax:

<%= FIELD COMPARISON VALUE ? TRUE : FALSE %>
<%$ FIELD COMPARISON VALUE ? TRUE : FALSE %>

The form that begins with $ instead of = allows you to have the > character in
your logic condition.

FIELD is a field name in the form, or an equation of field names and environment variables.

COMPARISON can be:

numeric comparisons: gt lt gte lte >= <= > <
text comparisons: eq ne = !=
substring searches: s =~ !~

VALUE is a number, quoted string (use ' or ", like in JavaScript and Perl), or
equation of field names and environment variables. If VALUE is an equation,
its first symbol should be a field name, and not a constant number.

If there is a ! character before FIELD, the inverse of the comparison will be used.

TRUE is text (quotes are optional), a field name, or an equation involving fields. For the latter to be recognized, write something of the form $Q1* 4 + Q2. The $ is the hint to the interpreter that you aren't using a plain text string. To use an environment variable, use $$.

Both FIELD and VALUE may be arithmetic equations involving field names and environment variables. If VALUE is an equation, it should start with a field name (not a number). Otherwise, only the first number in the equation will be used for the comparison.

JavaScript Interpreter:

You can directly access the JavaScript interpreter

<%
if (Q3 > 100) { Q4 = 5; print(Q6, 'hello!'); }
%>

Operations supported in the JS interpreter are:

if ... else if ... else
Code blocks need to be enclosed in curly braces, as in Perl.

Logical comparisons:

( ) ! && || are understood the same as in JavaScript
Perl, and the comparison operators are the same as listed above. Environment variables are accessible in the same way as mentioned before.

If... Else...:

This type of If Else statement is another way of printing text based on values.

<% val1=101; %>
<% val2=106; %>
<%if val1 lt val2 %>TRUE<%else %>FALSE<%endif %>
<%if val1 gt val2 %>TRUE<%else %>FALSE<%endif %>

The text "TRUE" or "FALSE" can be any text that choose. The syntax is this:


<%if condition %>true text<%else %>false text<%endif %>

Randomizing order:

Special function for randomizing order

<%random rnd end %>
Group 1<rnd>
Group 2<rnd>
Group 3<rnd>
Group 4<rnd>
Group 5<rnd>
Group 6<rnd>
Group 7<rnd>
Group 8<rnd>
<end>

You can put anything you want where it says Group 1, or any other group. The tag <rnd> must come after any group that you want randomized.

It will produce something like this:

Group 6 Group 3 Group 5 Group 7 Group 2 Group 8 Group 4 Group 1

Inserting Files:

You can insert an existing page into a page generated by the EZSurvey server program by typing the following code, this will include another report without evaluating any JavaScript code.

<%source filename %>

Where filename is the file on the server to be inserted when the survey is taken. This is particularly useful for including menus, banners, etc...

This will include another file while evaluating the JavaScript code:

<%include filename %>

In addition to what the previous tag does, this one can fill in any input (form field) that is contained in filename. The data could come from either the environment variables, participant, or a calculation.

For instance, if the filename has an input with a default value $HTTP_TIME, the displayed page will have the input with a default value set to the time the file was parsed by the program on the server.

Query:

You can print a formatted report from an ASCII file (the file database.asc, where database is the name of the form.) by typing the following code:

<%report database.asc query_string %>
<P>{Q1} {Q2} {Q3} {Q4}
</report>

Where database.asc is the name of an ASCII database file and query_string looks like FIELD=value&field=value. Altogether, it might look something like this:

<% value1={STATE}; %>
<%report cities.asc STATE=$value1 %>
<option value={CODE}>{LABEL}</option>
</report>

It isn't necessary to include every field in the query or even every field that will be referenced later. In the query, * and ? characters can be used as wild cards the same way they are used in DOS file names.

The {#} variable will count which number record you are on. The <%end%> command stops printing the report.

Sum File:

The .sum file will look something like this:

[COUNT]
Q2=4
Q2_1=1
Q2_2=4
Q2_3=1
Q2_4=2
Q2_5=1
Q3=6
[SUM]
Q3=350760689582706.000000

To print variables from the sum file, the code will look something like this:

{Q1_1}
{Q3.SUM}

INI File:

<%stats filename %>

Examples:

<%report test1.asc "ID='102'" %>
     {#}:{ID} <% print(val1) print(data['ID']) %>
</report>

<%report test1.asc %>
     {ID}{ID eq 101 ? 'TRUE':'FALSE' }
</report>

See also...