Nikolay Matyushkin2018-11-09 11:23:09
Nikolay Matyushkin, 2018-11-09 11:23:09

AJAX. Why does the entire document come back?

Task: It is necessary to make the implementation of adding messages without reloading the page.

  1. I made a form with a text input and a button, to which I attached an onclick event with the send_mesage () function
  2. Created an infoblock where all comments will be loaded
  3. Registered a block in which GetList will display all the elements of this very infoblock
  4. I wrote a script that, using ajax, will send data from the form to the same page.

It's all on one page and looks like this.

First comes js with the block reboot function, then the ajax data sending function (I did not write the creation of the getXmlHttpRequest object, but it is there, connected):

function mode(){
          url: location.href,
          success: function(data) {

  var message;
  function mess_add(){
    var objForms = document.forms.ajax_form[0].name + "=" + document.forms.ajax_form[0].value;

    message = getXmlHttpRequest();
    message.open("POST", location.href, false);
    message.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    message.onreadystatechange = function(){
      if(message.readyState == 4){
        if(message.status == 200){


Next, we create an element in the infoblock

  $elCom = new CIBlockElement;

  $PROP = array();
  $PROP["COMMENTS"] = $_POST["comments_text"];
  $PROP["ID_EVENT"] = $arResult["ID"];

  $fieldsComments = Array(
    "DATE_CREATE" => date("d.m.Y H:i:s"), //Передаем дата создания
    "CREATED_BY" => $USER->GetID(),
    "IBLOCK_ID"      => 33,
    "NAME"           => "unknow",
    "ACTIVE"         => "Y",            // активен
  $IDcom = $elCom->Add($fieldsComments);
    // LocalRedirect("/youdo/event_view/detail.php?ID=".$arResult["ID"]);

Next comes the block with the output of messages

<div id="result_form">
      <table style="width: 100%;">
        <?	$arSelect = Array("ID", "IBLOCK_ID", "DATE_CREATE");
          $arFilter = Array("IBLOCK_ID" => 33, "ACTIVE"=>"Y", "PROPERTY_ID_EVENT" => $arResult["ID"]);
          $res = CIBlockElement::GetList(Array(), $arFilter, false, Array(), $arSelect);
          while($final_array = $res->GetNextElement()){
            $arFields = $final_array->GetFields();
            $arProps = $final_array->GetProperties();
            $rsUser = CUser::GetByID($arProps["USER_AUTHOR"]["VALUE"]);
            $arUser = $rsUser->Fetch();
            $arImage = CFile::GetPath($arUser['PERSONAL_PHOTO']);?>
            <td rowspan="2" style="width: 110px;"><?if(!empty($arImage)):?><img src="<?=$arImage;?>" alt=""><?else:?><img src="<?=CUtil::GetAdditionalFileURL(SITE_TEMPLATE_PATH.'/image/user.png')?>" alt=""><?endif;?></td>
            <td style="color: #343F4B;"><?=$arUser["NAME"]." ".$arUser["LAST_NAME"];?></td>
            <td style="width: 15%; text-align: right; font-size: 12px; color: silver;"><?=$arFields["DATE_CREATE"];?></td>
            <td colspan="2" style="color: #8492A6;"><?=$arProps["COMMENTS"]["VALUE"];?></td>

And the form itself.

<form action="" method="get" id="form_message_add">
<input type="text" name="message">
<button onclick="send_message()">Отправить</button>

Everything is OK. Elements are added, but the first response to the specified block with id result_form is the entire page. The block is rebuilt, but not the updated data is placed there, but the entire page. Who will guide you on the right path? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Ainur Valiev, 2018-11-09


Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question