Survey Countdown
Suppose you required the results of 1000 surveys before
making a decision about something. Suppose further, that you
wanted to publish how many more surveys are still required to
reach that number.
I'll show you how to do that countdown.
Or, suppose you have a special promotion and want to
award the first 100 survey takers. Suppose further that you
want to publish the countdown to encourage early
participation.
The same system can be used to do that countdown.
Master Survey keeps track of how many times the survey has
been taken. The system presented in this article taps into
that information.
This works with Master Survey version 2.8a and later. (The
lastest version is 2.8c of August 6, 2007.)
How It Works
You put some JavaScript on your web page where you want the
number printed for how many surveys are left to go. The
JavaScript's SRC URL points to a script that provides the
information. (The script can be Perl or PHP. Both are
provided here.)
When the JavaScript requests the information from the
script, the script finds out how many surveys have already
been taken, subtracts that amount from the target amount,
and sends the resulting number to the web page.
When the target has been reached, "(completed)" is printed
instead of the number left to go.
You must, of course, have a survey created before
implementing this.
The Template
Master Survey allows a results template to be specified
where cumulative results are printed for those who have
taken the survey. It's optional.
If you use results a template, you'll need to insert this
somewhere within it:
Do that only if you use a results template.
The Script
Now, decide whether you prefer to use Perl CGI or PHP.
Both the Perl and the PHP scripts are below. Use one or the
other, not both. Customization information follows the
scripts.
The Perl script requires LWP::Simple be installed on your
server. Here is the script:
The PHP script requires the file_get_contents() function be
able to get web pages with an absolute http://... URL. Here
is the script:
Whichever script you decide to use, it will require these
two customizations.
-
The target number (variable $TargetNumber)
Specify how many surveys are your target.
-
The survey results URL (variable $SurveyResultsURL)
To find the survey results URL, see the Master Survey
control panel. Select the appropriate survey and
click the "Generate Survey Form" button. On the next
page, at the second note, is a form field with a URL.
That URL is the survey results URL. (You do not need
to re-generate the survey form, just copy the URL at
that second note.)
The script may be installed on any domain.
If installing the PHP script, verify the PHP code is at the
beginning of the file. No spaces or other characters, and no
blank lines, may come before the PHP code. Otherwise, the
PHP code will be unable to send the JavaScript header line
to the browser.
If installing the Perl script, upload it to your server as a
plain text file (not binary), in a directory that can run
Perl scripts. Then, give the script 755 permissions.
Once the script of your choice has been installed, type its
URL into your browser to verify error-free operation. (If
the web page is blank, and if you're curious, "view source"
to see what the script sent to the browser.)
The JavaScript
The last step is putting the JavaScript into your web page
where you want the number to be printed.
JavaScript code plays havoc with some email programs, and
some web mail might not display it at all. Therefore, if you
have any doubt as to the correctness of the JavaScript, see
the online version of the article
here.
Here is the JavaScript:
Replace http://example.com/myscript.php with the URL to the
Perl CGI or PHP script, whichever you decided to use.
Put the JavaScript into your web page where you want the
number to be printed. Here is an example:
The JavaScript doesn't have to be on the same page as the
survey form, but it may. The web page with the JavaScript
may be on any domain.
The System
You now have the system installed.
Whenever the web page with the JavaScript loads, it prints
the number of surveys left to take before the target is
reached. Once the target is reached, it prints
"(completed)".
The "Caption This Cartoon" survey web page
has a live example of the system in use.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©Copyright 2007 Bontrager Connection, LLC Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.