I
I
I'm Yoda2017-09-23 15:21:14
PHP
I'm Yoda, 2017-09-23 15:21:14

How to pass in an Ajax request an array of the current element formed by the PhpToJSObject method?

Hello connoisseurs!
Who will tell you how to pass the array of the current element in the Ajax request. You can make it easier and push the script into the foreach loop, but I want to limit myself to the script outside the loop. I also tried to push this array into a hidden input with the assignment id="item_<?= $arItem["ID"]; ?>" , and get it like this:

var ID = $(this).attr('id'); // Берется из id кликнутого элемента
var ITEM = $('#item_' + ID); // Собственно сам скрытый инпут с подстановкой полученного id

In theory, everything (correct). But for some reason, the formed array ignores the fact that the input is hidden and is displayed on the page. At the moment I did the following:
Convert PHP array to JS
$rsItemJS = array(
            "ID" => $arItem["ID"],
            "NAME" => $arItem["NAME"],
            "PROPERTIES" => $arItem["PROPERTIES"]
        );

        $arItemJS = CUtil::PhpToJSObject($rsItemJS);

Next, in the JS script, you need to pass this array! There can be many elements, how to pass exactly the one on which the click was made?
$(document).ready(function ($) {
        $('.item').on('click', function () {

            var ITEM = <?= $arItemJS ?>;
            var AjaxSelectItem = {ITEM: ITEM};

            ajaxrequest('/include/popup.modal.php', AjaxSelectItem , ' .modal-body');

            return false;
        });
    });

Actually the script.js file itself, which contains the ajaxrequest function
function ajaxrequest(url, param, content) {
    $.ajax({
        type: "POST",
        url: url,
        data: param,
        success: function (response) {
            $(content).html(response);
        }
    });
}

Who has any ideas? Where do I need to dig, who will give me the right vector for the solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Yanyshev, 2017-09-23
@Anadi

Bringing elements to the front:

...
<div class="item" data-id="<?= $arItem['ID'] ?>" > тут всякое </div>
...

Then broadcast the event:
$('.item').click(function() {
 ajaxrequest('/include/popup.modal.php',  {item_id: $(this).data('id')} , ' .modal-body');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question