Bitrix Site Manager

CFormResult::Add

int
CFormResult::Add(
 int WEB_FORM_ID,
 array arrVALUES=false
 char(1) CHECK_RIGHTS=Y
 integer USER_ID=false
);

Saves a new result in the database.

Parameters

ParameterDescription
WEB_FORM_ID The web form ID.
arVALUES Array of input values. In this parameter is false or not specified, it is taken from the array stored in $HTTP_POST_VARS. If $HTTP_POST_VARS is empty, it is taken from the array $HTTP_GET_VARS.
CHECK_RIGHTS If set to "Y", user permissions will be checked when adding a result. If "N", no checks are performed.
USER_ID The ID of a user who created this result. By default, the ID of the current user is recorded.

Return Values

Returns the ID of the saved result on success, or 0 otherwise.

Example

The following is an example of array $HTTP_POST_VARS passed to arrVALUES.


Array
(
    [form_text_586] => John Smith
    [form_date_587] => 10.03.1992
    [form_textarea_588] => London
    [form_radio_VS_MARRIED] => 589
    [form_checkbox_VS_INTEREST] => Array
        (
            [0] => 592
            [1] => 593
            [2] => 594
        )

    [form_dropdown_VS_AGE] => 597
    [form_multiselect_VS_EDUCATION] => Array
        (
            [0] => 603
            [1] => 604
        )

    [form_text_606] => 2345
    [form_image_607] => 1045
)

The below example shows the sample file "form_fill.php" displaying the form.


<?
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
if (CModule::IncludeModule("form"))
{
    if (intval($WEB_FORM_ID)>0) 
        $z = <b>CForm::GetByID</b>($WEB_FORM_ID); 
    else 
        $z = CForm::GetBySID($WEB_FORM_NAME);

    if (!($zr=$z->Fetch())) 
    {
        $FORM_EXIST = "N";
        $strError .= "Invalid form ID.<br>";
    }
    else
    {
        $FORM_EXIST = "Y";
        $WEB_FORM_ID = $zr["ID"];
        $WEB_FORM_NAME = $zr["VARNAME"];
        if ($REQUEST_METHOD=="POST" && 
            intval($WEB_FORM_ID)>0 && 
            strlen($web_form_submit)>0)
        {
            $arrVALUES = $HTTP_POST_VARS;
            $error = CForm::Check($WEB_FORM_ID);
            if (strlen($error)<=0)
            {
                if ($RESULT_ID = CFormResult::Add($WEB_FORM_ID))
                {
                    $arr = CFormResult::GetDataByID($RESULT_ID, array("VS_PHOTO"));
                    $PHOTO_EXIST = (intval($arr["VS_PHOTO"][0]["USER_FILE_ID"])>0) ? "Y" : "N";
                    CFormResult::SetField($RESULT_ID, "VS_PHOTO_EXIST", $PHOTO_EXIST);
                    CForm::SetEvent($RESULT_ID);
                    CForm::Mail($RESULT_ID);
                    LocalRedirect("index.php?WEB_FORM_NAME=".$WEB_FORM_NAME. "&strNote=".urlencode("information saved"));
                }
            }
            else 
                $strError .= $error;
        }
    }
}

if (strlen($APPLICATION->GetTitle()) <= 0)
    $APPLICATION->SetTitle("Visitor survey");

require_once ($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_after.php");
echo ShowNote($strNote);
echo ShowError($strError);
?><?
if (CModule::IncludeModule("form"))
    CForm::Show($WEB_FORM_NAME, $arrVALUES); 
require_once ($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog.php");
?>