SoSci Survey offers a wide selection of question types – however, sometimes these are insufficient to achieve the desired appearance required for a particular question. If this is the case, SoSci Survey is able to read custom (HTML) form elements as well. JavaScript or Flash based questions can easily be integrated in this way as well.
These tools give the user a lot of freedom when designing the questionnaire – however, the complexity involved in programming form elements can quickly get out of hand.
Tip: Before you start programming your own forms, please check to see if there is a simpler option in Advanced Display Options.
HTML form elements (e.g. text input fields, checkboxes, drop-downs) can be programmed quickly: the essentials can be found on SelfHTML (German only). Inserting HTML code into the questionnaire is quite simple with a text element (save the HTML code as a text element and drag into the questionnaire).
The problem is that SoSci Survey does not initially recognize any form elements and the data they transmit, and as a result it does not save the responses. Therefore, it is advisable to inform SoSci Survey that certain POST variables should be read and saved in the data record. Freely placed input fields are used to do so.
name
) of the form element. Therefore, if your variable's ID is „AB01_01“, then name="AB01_01"
is written in your form element. prepare_input()
in order to activate the internal variables, e.g. prepare_input('AB01_01');
. If you specfy the question ID (e.g. prepare_input('AB01');
), all variables in the question will be activated. If you program more sophisticated input elements – e.g. with JavaScript and/or Flash – then, as a rule, you need to have hidden input fields to store the measured/collected values (see Note when Browser Window Changes).
In this instance, do not use the PHP function prepare_input()
; drag the „internal variables“ question type directly into the questionnaire page instead. This generates hidden input fields in the questionnaire (<input type="hidden">
) automatically, and their values can be changed using JavaScript. The HTML ID of the input field corresponds to the item ID.
There are two major limitations when using custom form elements:
value()
and insert placeholders into the HTML code for the question. SoSci Survey allows a maximum of 99 options in a normal selection question. This also applies to selection options in a drop-down field. This amount is not enough to list all countries in the world. Using the following HTML code, an input box can be created that supports this.
Tip: A drop-down selection with more than 99 options can also be created with the question type Extended Selection. A pre-existing selection question can be converted into an extended selection in the tab Extended.
<select name="AB01_01"> <option value="1">Afghanistan</option> <option value="2">Albania</option> <option value="3">Algeria</option> <!-- etc. --> <option value="206">Zimbabwe</option> </select>
Tip: If you have the selection options to hand in Excel or OpenOffice Calc, the function CONCATENATE()
can automatically create the majority of the HTML code. If, for example, the answer code is in column A, and the text in column B, enter the following formula into column C (first row). Then, drag the formula across the whole of column C, and simply copy the code created as a result.
=CONCATENATE("<option value=""";A1;""">";B1;"</option>")
AB01_01
was entered here as the name for the form element select
– this must be adjusted to the actual item ID (of the item in the „internal variables“ question). An attribute value
is specified for each selection option option
– this determines which numeric code will be submitted. If you want the actual text to be stored, just omit the attribute.
Only the corresponding PHP code is now missing – the ID must also be adjusted here:
prepare_input('AB01_01');
Done. Almost, at least.
At the moment, the drop-down would just be „stuck“ on the left-hand side of the page. The following table gives it a little more style:
<table cellspacing="0" cellpadding="0" border="0"> <colgroup> <col width="100"> <col> </colgroup> <tr> <td> <label for="AB01_01">Native Country:</label> </td> <td> <select name="AB01_01" id="AB01_01"> <option value="1">Afghanistan</option> <option value="2">Albania</option> <option value="3">Algeria</option> <!-- etc. --> <option value="206">Zimbabwe</option> </select> </td> </tr> </table>
To stay with the style of the drop-down fields in SoSci Survey, the CSS classes from SoSci Survey can be used as well:
<div class="spacing"> <div class="title"> <p>Question title here</p> </div> <div class="titleSpacing"></div> <table class="question" cellspacing="0" cellpadding="0" width="100%" border="0"> <colgroup> <col width="380"> <col> </colgroup> <tr class="shadeH0"> <td><label for="AB01_01">Native Country:</label></td> <td class="dropdown input"> <select name="AB01_01" id="AB01_01"> <option value="1">Afghanistan</option> <option value="2">Albania</option> <option value="3">Algeria</option> <!-- etc. --> <option value="206">Zimbabwe</option> </select> </td> </tr> </table> </div>
Tip: The CSS classes that SoSci Survey uses can just be looked up in a „normal“ question: the HTML source code can be displayed in the browser by going on the respective page in the questionnaire. The source code for a particular question can easily be found by searching for the question ID.
The solution outlined above assumes that the value from the custom input element (<input>
or <select>
) will be submitted into the data record.
However, it may be the case that you do not want to create this type of input field at all, and want to determine the value using JavaScript instead, and submit this value into the data record. Two changes are necessary if this is the case:
prepare_input()
from the questionnaire page.
Tip: If you only require certain internal variables of the question, set this with the symbol in Show items. Alternatively, insert the question type „internal variables“ onto the page using the PHP function question()
and specify the respective item IDs.
There is no visual change when you insert the question into the questionnaire. However, there will be a hidden input field (type="hidden"
) stored for each internal variable. The content of these fields can be read and changed with JavaScript.
// Use variables 1 und 2 from question AB01 (type "internal variables") question('AB01', '1,2');
<script type="text/javascript"> <!-- // Save value in variable 1 document.getElementById("AB01_01").value = "value A"; // Increase last value in variable 2 by 10 var input2 = document.getElementById("AB01_02"); var curVal = parseInt(input2.value); if (isNaN(curVal)) { input2.value = 10; } else { input2.value = curVal + 10; } // --> </script>