A
A
Alexander Ivanov2018-02-07 11:08:54
JavaScript
Alexander Ivanov, 2018-02-07 11:08:54

Why is the data sent by a Get request and not a post as I indicated?

The form

<?php
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$APPLICATION->SetTitle("Добавление акции");
?>
  <form id="formPromo">
      Название:<input name='name'/><br>
      Дата:<input name='date' type ='date'/><br>
      Описание:<textarea name='discription'></textarea><br>
      Ссылка:<input name='addres'/><br>
      Картинка:<input name='picture' type ='file'/><br>
      <input value='Добавить' type='submit'>
  </form>
 <?php require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");?>

Jquery
(function ($) {
jQuery(document).ready(function ($) {
$("#formPromo").submit(function () {        
            var form_data = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "/promo/edit/promo.php",
                data: form_data,
                success: function () {
                    alert('Форма успешно отправлена!');           
                }
            });
            return false;
        });
});
               })(jQuery);


Handler
<?php

$APPLICATION->SetTitle("Title");

if(isset($_POST['name'])&&$_POST['name']!="")
{
$name = htmlentities($_POST['name']);
//$date = htmlentities($_POST['date']);
//$discription = htmlentities($_POST['discription']);
//$addres = htmlentities($_POST['addres']);
//$picture =  htmlentities($_POST['picture']);
 

if (CModule::IncludeModule('iblock')){
            $el = new CIBlockElement;

            $arProp["NAME"] = $name;  
      
           
           $arFields = array(
                'NAME' => $fio."-".$mail,               
                'MODIFIED_BY' => $USER->GetID(),             
                'IBLOCK_ID' => 13,
                'ACTIVE' => 'Y',


                'PROPERTY_VALUES' => $arProp
            );

            $intOfferID = $el->Add($arFields);
}

}
?>


The problem is that the request is not sent by post, although the method is explicitly specified in the script. Despite this, it is sent via get.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Kuzmenko, 2018-02-07
@alexsteadfast

I'm assuming that when the "Add" button is clicked, the form is sent to the server before the JS is processed.
Alternatively, you can replace it with Also edit the handler:

$("#submit").click(function () {        
            var form_data = $("#formPromo").serialize();
            $.ajax({
                type: "POST",
                url: "/promo/edit/promo.php",
                data: form_data,
                success: function () {
                    alert('Форма успешно отправлена!');           
                }
            });
            return false;
        });
});

O
Oleg Polyakov, 2018-02-07
@challenger1401

$(this).serialize(); returns a string. and data should have an object

E
evgennikolaevich, 2018-02-07
@evgennikolaevich

Try like this:
<form id="formPromo" method="post">

A
Ainur Valiev, 2018-02-07
@vaajnur

try along with (for versions below 1.9.0) specify (version added: 1.9.0)

S
Stepan, 2021-09-29
@steff

Perhaps it will help someone: just in case, check the order of including JS files.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question