Documentation

GetDropDownField

string
CForm::GetDropDownField(
 string question_sid,
 array list,
 mixed value = "",
 string add_to_dropdown = "class=\"inputselect\""
)

The method returns HTML code of single selection dropdown list, intended to select answer from the"dropdown" type group of answers to a question, which ID is passed in the parameter question_sid.

The method can be used in both the form for creation of a new result, and in the form of editing an existing result. Non-static method.

Note
Name of resulting HTML field will be generated by the following mask:
form_dropdown_question_sid

Method parameters

Parameter Description Available from version
question_sid Symbolic identifier of question.
list Array of "dropdown" type answers to a question with question_sid. Minimally-required structure of this array:
Array
(
    [reference] => Array
        (
            [0] => answer title 1
            [1] => answer title 2
            [2] => answer title 3
            ...
        )

    [reference_id] => Array
        (
            [0] => answer ID 1
            [1] => answer ID 2
            [2] => answer ID 3
            ...
        )
)
The parameter ANSWER_TEXT of answer is used as the item title in this array.
value If this parameter passes array with value that match answer ID, this answer will be selected in the resulting dropdown list:
<option value="element value" selected>answer title</option>

Optional parameter. Empty by default.
add_to_dropdown Custom HTML to be added in the resulting HTML tag:
<select add_to_dropdown ...>

Optional parameter. By default - "class=\"inputselect\"".

See Also

Examples of use

<?
/*******************************************
       Result edit
*******************************************/

$RESULT_ID = 12; // result ID

// if the "Save" is clicked...
if (strlen($_REQUEST["save"])>0)
{
    // use data, received from the web form  
    $arrVALUES = $_REQUEST; 
}
else
{
    // generate the array from the result data 
    $arrVALUES = CFormResult::GetDataByIDForHTML($RESULT_ID); 
}
?>
<form action="" method="POST">
<table>
    <tr>
        <td>*Age:</td>
        <td><?

            // question ID
            $QUESTION_SID = "AGE"; 

            // array describing dropdown list items
            $arDropDown = array (

                "reference" => array (
                        "-",
                        "10-19",
                        "20-29",
                        "30-39",
                        "40-49",
                        "50-59",
                        "60 and older"
                    ),

                "reference_id" => array (
                        608,
                        596,
                        597,
                        598,
                        599,
                        600,
                        601
                    ),

                "param" => array (
                        "not_answer class=\"inputselect\"", // is not an answer
                        "",
                        "checked", // default value
                        "",
                        "",
                        "",
                        ""
                    )
            );

            // get current value of dropdown list
            $value = CForm::GetDropDownValue($QUESTION_SID, $arDropDown, $arrVALUES);

            // display dropdown list
            echo CForm::GetDropDownField(
                $QUESTION_SID,           // question ID 
                $arDropDown,             // array describing the list items 
                $value,                  // value of selected list item
                "class=\"inputselect\""  // style of dropdown list
                );            
            ?></td>
    </tr>
</table>
<input type="submit" name="save" value="Save">
</form>


© «Bitrix24», 2001-2024