D
D
De-122020-08-28 18:30:06
JavaScript
De-12, 2020-08-28 18:30:06

Why sends 2 times?

When sending forms from the site, they are added 2 times to the infoblock and 2 times sent to the mail, respectively, I can’t understand what’s wrong, the js and php handler is below:

JS
case 'callback':
         $('#callback-button').bind('click', function() {
            if ($('#callback-popup').hasClass('visible')) {
               $('#callback-popup').removeClass('visible').addClass('invisible');
            } else {
               $('#message').hide();
               $('input').removeClass('error');
               $('#callback-popup').removeClass('trans-delay invisible').addClass('visible');
            }
            return false;
         });
         $('#callback-popup .close').bind('click', function() {
            $('#callback-popup').removeClass('visible').addClass('invisible');
            return false;
         });
         $('#callback').bind('submit', function() {
            $('#message').hide();
            $('.input input, .input textarea').removeClass('error');
            var has_errors = false;
            if ($('#o1').val() == '') {
               has_errors = true;
               $('#o1').addClass('error');
            }
            if ($('#o2').val() == '') {
               has_errors = true;
               $('#o2').addClass('error');
            }
            if (has_errors) {
               $('#message').css('color', '#c63300').text('Заполните выделенные поля.').show();
            } else {
               $.ajax({
                  url : '/forms/callback.php',
                  data : $(this).serializeArray(),
                  dataType : 'json',
                  type : 'POST',
                  cache : false,
                  success : function(answer) {
                        console.log(answer);
                        $('#message').css('color', '#48698c').text('Ваш заказ отправлен.').show();
                        $('#callback').trigger('reset');
                        //setTimeout("console.log('go')", 1000);
                        $('#callback-popup').removeClass('visible').addClass('trans-delay invisible');
                  },
                  error : function(answer) {
                        console.log(answer);
                     /*$('#message').css('color', '#c63300').text('Ошибка отправки. Попробуйте позже.').show();*/
                        $('#message').css('color', '#48698c').text('Ваш заказ отправлен.').show();
                        $('#callback').trigger('reset');
                        //setTimeout("console.log('go')", 1000);
                        $('#callback-popup').removeClass('visible').addClass('trans-delay invisible');

                  }
               });
            }
            return false;
         });
         break;


      case 'constructor':
         $('#constructor-button').bind('click', function() {
            if ($('#constructor-popup').hasClass('visible')) {
               $('#constructor-popup').removeClass('visible').addClass('invisible');
            } else {
               $('#message').hide();
               $('input').removeClass('error');
               $('#constructor-popup').removeClass('trans-delay invisible').addClass('visible');
            }
            return false;
         });
         $('#constructor-popup .close').bind('click', function() {
            $('#constructor-popup').removeClass('visible').addClass('invisible');
      $(".col-md-12 .blok").removeClass("show");
      $(".typebloka").removeClass("hide");
      $(".constrbegin").removeClass("show");
      var pageurl = $("#pageurl").val();
      setTimeout(document.location.replace(pageurl), 500);
            return false;
         });
         $('#constructor').bind('submit', function() {
            $('#message').hide();
            $('.input input, .input textarea').removeClass('error');
            var has_errors = false;
            if ($('#o1').val() == '') {
               has_errors = true;
               $('#o1').addClass('error');
            }
            if ($('#o2').val() == '') {
               has_errors = true;
               $('#o2').addClass('error');
            }
            if (has_errors) {
               $('#message').css('color', '#c63300').text('Заполните выделенные поля.').show();
            } else {
               $.ajax({
                  url : '/forms/callback.php',
                  data : $(this).serializeArray(),
                  dataType : 'json',
                  type : 'POST',
                  cache : false,
                  success : function(answer) {
            console.log(answer);
                        $('#message').css('color', '#48698c').text('Ваш заказ отправлен.').show();
                        $('#callback').trigger('reset');
                        //setTimeout("console.log('go')", 1000);
                        $('#callback-popup').removeClass('visible').addClass('trans-delay invisible');
                  },
                  error : function(answer) {
            console.log(answer);
                     /*$('#message').css('color', '#c63300').text('Ошибка отправки. Попробуйте позже.').show();*/
                        $('#message').css('color', '#48698c').text('Ваш заказ отправлен.').show();
                        $('#callback').trigger('reset');
                        //setTimeout("console.log('go')", 1000);
                        $('#callback-popup').removeClass('visible').addClass('trans-delay invisible');

                  }
               });
            }
            return false;
         });
         break;



      case 'buy':
         $('.js-buy').bind('click', function() {
            var text = $(this).attr('data-service');
            $('.js-buy-form .js-title').text(text);
            $('.js-buy-form .js-service').val(text);
            $('.js-buy-form .js-message').hide();
            $('.js-buy-form').removeClass('invisible').addClass('visible');
            return false;
         });
         $('.js-buy-form .js-close').bind('click', function() {
            $('.js-buy-form').removeClass('visible').addClass('invisible');
            return false;
         });
         $('.js-who label').bind('click', function() {
            var is_org = $(this).attr('data-org');
            if (is_org == '1') {
               $('.js-org').show();
            } else {
               $('.js-org').hide();
               $('.js-org input').val('');
            }
            return true;
         });
         $('.js-buy-form form').bind('submit', function() {
            var has_errors = false;
            $('.js-buy-form .js-required').removeClass('error').each(function() {
               if ($(this).is(':visible') && $(this).val() == '') {
                  has_errors = true;
                  $(this).addClass('error');
               }
            });
            var $mess = $('.js-buy-form .js-message');
            $mess.hide().removeClass('error');
            if (has_errors) {
               $mess.addClass('error').text('Заполните выделенные поля.').show();
            } else {
               $.ajax({
                  url : '/forms/buy.php',
                  data : $(this).serializeArray(),
                  dataType : 'json',
                  type : 'POST',
                  cache : false,
                  success : function(answer) {
                     if (answer.ok) {
                        $mess.text('Ваш заказ отправлен.').show();
                        $('.js-buy-form').removeClass('visible').addClass('trans-delay invisible').on('transitionend', function(){
                           $(this).off('transitionend');
                           $('.js-buy-form form').trigger('reset');
                           $('.js-org').show();
                        });
                     } else {
                        $mess.addClass('error').text('Ошибка отправки. Попробуйте позже.').show();
                     }
                  },
                  error : function() {
                     $mess.addClass('error').text('Ошибка отправки. Попробуйте позже.').show();
                  }
               });
            }
            return false;
         });
         break;


callback.php example
<?php
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
use Bitrix\Main\Mail\Event; 
$MESS = "";
foreach($_REQUEST as $k=>$v)
{
  $name = $k;
  switch($k)
  {
    case "o1": $name = "ФИО"; break;
    case "o2": $name = "Телефон";break;
    case "o3": $name = "Сообщение";break;
  }
  $MESS.= "<b>".$name.":</b> ".$v."<br>";
}
Event::sendImmediate(array( 
    "EVENT_NAME" => "CALLBACK", 
    "LID" => "s1", 
    "C_FIELDS" => array( 
        "MESS" => $MESS,
    ), 
));

CModule::IncludeModule("iblock");
$el = new CIBlockElement;
$PROP = array();
$PROP[52] = $_REQUEST["o1"];
$PROP[53] = $_REQUEST["o2"];
$arLoadProductArray = Array(
  "MODIFIED_BY"    => 1, // элемент изменен текущим пользователем
  "IBLOCK_SECTION_ID" => 91,
  "IBLOCK_ID"      => 9,
  "PROPERTY_VALUES"=> $PROP,
  "NAME"           => "Форма обратный звонок в шапке",
  "ACTIVE"         => "Y",            // активен
  "DETAIL_TEXT"    => $_REQUEST["o3"],
  );
$ID = $el->Add($arLoadProductArray);
?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question