How to make ajax form and storing results in the admin?

Hello!
How best to implement a form(ajax) to the results stored in the admin and to the email of the administrator(or at written separately in the mail) came to notice with the completed data.
the Internet and came across different implementations and using main:feedback and using iblock.element.add and through bittrix:form.result.new. but since I don't really Bitrix - I don't quite understand how better and more correctly.

Thank you)
March 19th 20 at 08:28
1 answer
March 19th 20 at 08:30
Solution
You asked a lot of questions in one post.
Here's a great example:
<?
$el = new CIBlockElement;
$PROP = array();
$PROP[12] = "White"; // property with code 12 is assigned the value "White"
$PROP[3] = 38; // property with code 3 is assigned the value 38
$arLoadProductArray = Array(
 "MODIFIED_BY" => $USER->GetID(), // element modified by the current user
 "IBLOCK_SECTION_ID" => false, // the element is at the root of the partition
 "IBLOCK_ID" => 18,
 "PROPERTY_VALUES"=> $PROP,
 "NAME" => "Item",
 "ACTIVE" => "Y", // active
 "PREVIEW_TEXT" => "text for the list items",
 "DETAIL_TEXT" => "text for detailed view"
 "DETAIL_PICTURE" => CFile::MakeFileArray($_SERVER["DOCUMENT_ROOT"]."/image.gif")
);
if($PRODUCT_ID = $el->Add($arLoadProductArray))
 echo "New ID: ".$PRODUCT_ID;
else
 echo "Error: ".$el->LAST_ERROR;
?>

Create an InfoBlock, it properties.
Create a page, it form fields when you submit a form do a post request to the page with the code above.
Here is an example for ajax
<script type="text/javascript">
$(function () {
$('#form_id').submit(function(e){
e.preventDefault();
 var data = $(this).serialize();
 $. ajax ({
url:'/ajax.php',
type:'post',
data:data,
success:function(res){
$('#result').html(res);
}
})
})
 }) 
 </script>

In response to get New ID.
Mail attach through the file init.php adding action event OnAfterIBlockElementAdd
Yes! This is what you need! The elements in the information block are added
only now faced with a problem in unit #result returns the following:
"When you run the script the error occurred. Enable error output in the settings file .settings.php"
include debug and unit #result returns the correct value "New ID: ..."
the code in ajax.php
<?php
require_once $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php";
use Bitrix\Main\Context;

$request = Context::getCurrent()->getRequest();
$_contact_us_name = $request["contact_us_name"];
$_contact_us_phone = $request["contact_us_phone"];
$_contact_us_email = $request["contact_us_email"];
$_contact_us_message = $request["contact_us_message"];

$el = new CIBlockElement;
$PROP = array();
$PROP[97] = $_contact_us_name;
$PROP[98] = $_contact_us_phone;
$PROP[99] = $_contact_us_email;
$PROP[100] = $_contact_us_message;
$arLoadProductArray = Array(
 "IBLOCK_SECTION_ID" => true,
 "IBLOCK_ID" => 13,
 "PROPERTY_VALUES"=> $PROP,
 "NAME" => "Application ".date("d.m.Y H:i"),
 "ACTIVE" => "Y",
 "PREVIEW_TEXT" => "",
 "DETAIL_TEXT" => "",
 "DETAIL_PICTURE" => "
);
if($PRODUCT_ID = $el->Add($arLoadProductArray))
 echo "New ID: ".$PRODUCT_ID;
else
 echo "Error: ".$el->LAST_ERROR;
require_once $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php"; 
?>

- damon commented on March 19th 20 at 08:33
take it away and just try to populate with data, the name of the properties you have in numbers?
use Bitrix\Main\Context;

$request = Context::getCurrent()->getRequest();
$_contact_us_name = $request["contact_us_name"];
$_contact_us_phone = $request["contact_us_phone"];
$_contact_us_email = $request["contact_us_email"];
$_contact_us_message = $request["contact_us_message"];


@damon, the code from a real project.

$el = new CIBlockElement;
$PROP = array();
$PROP[FITTING_STATUS] = Array("VALUE" => 145 ); 
$PROP[FITTING_CLIENT] = $_POST['name']; 
$PROP[FITTING_PHONE] = $_POST['phone'];
$PROP[FITTING_EMAIL] = $_POST['email']; 
$PROP[FITTING_ADRESS] = $_POST['adress'];
$PROP[FITTING_CITY] = $cityName;
$PROP[FITTING_ORDER] = array($fittingorder[0], $fittingorder[1], $fittingorder[2]);
$ORDER_DATE = 'Order from '.date("d.m.y H:i");
$arLoadProductArray = Array(
 "MODIFIED_BY" => $USER->GetID(), // element modified by the current user
 "IBLOCK_SECTION_ID" => false, // the element is at the root of the partition
 "IBLOCK_ID" => 28,
 "PROPERTY_VALUES"=> $PROP,
 "NAME" => $ORDER_DATE,
 "ACTIVE" => "Y", // active
 "PREVIEW_TEXT" => "",
 "DETAIL_TEXT" => "",
 "DETAIL_PICTURE" => "",
);
if($PRODUCT_ID = $el->Add($arLoadProductArray))
 echo "Order for the appointment number: ".$PRODUCT_ID.' accepted!';
else
 echo "Error: ".$el->LAST_ERROR;
- liliane.Hodkiewicz99 commented on March 19th 20 at 08:36
@liliane.Hodkiewicz99,
have properties instead of numbers prescribed character code
added
CModule::IncludeModule('iblock');
before
$el = new CIBlockElement;
and everything worked as it should
Thank you! - damon commented on March 19th 20 at 08:39

Find more questions by tags 1C-Bitrix