====== preset() ======

''void **preset**(string //variable//, int|string //code//)''

An answer option can be preselected with the function ''preset()''. Text can also be specified as an answer for text input questions.  

  * //variable//\\ The ID of the variable belonging to the input field. You can find the the correct variable ID in the **Variables Overview**.
  * //code//\\ For closed questions, the (numeric) answer code; for open questions, the text that should be preset.
      * You can find the codes for the selection options for selections, scales, ... in the **Variables Overview** (please see [[:en:results:values|Encoding and Output Values]]). 
      * Text must be written in quotation marks.


===== Tips =====

**Note:** ''preset()'' must be used on the same page as the question for which an answer should be specified.

**Note:** ''preset()'' must be called before (above) the question, for which the preset should apply.

**Note:** For a simple selection, the variable ID corresponds to the question ID. The ID of option to be selected is to be given as the value. 

**Note:** If an option is preselected for a drop-down selection, the questionnaire does not display the default option "[Please select]". Add a fallback option ("don't know") in the question. 

**Tip:** You can also preset text for free input fields in a (multiple-choice) selection ([[:/de:create:selection-textinput|Free Text Inputs Within a Selection]]). Use the exact variable ID for this, as can be read in the **Variables Overview**.

===== Examples =====

In a multiple-choice selection "MF01", the first and second options should be preselected. The following PHP code must be placed before/above the question.

<code php>
preset('MF01_01',2);
preset('MF01_04',2);
</code>

In the selection "EA01", the third option should be preselected.

<code php>
preset('EA01', 3);
</code>

The slider with the ID "SR01_01" with a differentiation ranging from 1 to 101 should be preset to 50% (valye:51).


<code php>
preset('SR01_01', 51);
</code>

The text "n/s" (not specified) should be preset for the free text input "TE01_01":

<code php>
preset('TE01_01', 'n/s');
</code>

In a question battery "SB01" with 20 scale items, the first option shall be pre-selected. Instead of typing 20 ''preset()'' commands, a FOREACH loop can be used.

<code php>
$items = getItems('SB01', 'all');
foreach ($items as $item) {
  $itemID = 'SB01_'.sprintf('%02d', $item);
  preset($itemID, 1);
}
</code>