L
L
lotoseu2021-05-26 15:17:39
AJAX
lotoseu, 2021-05-26 15:17:39

How to pass data from js to php using ajax?

There are dynamically rendered radio buttons

foreach ($offers as $element) {
                    $offer = $res[$arResult['ID']][$element['ID']];
                    // echo "<br>Цена ".$element['ITEM_PRICES'][0]['PRINT_PRICE']."  ".$res[$arResult['ID']][$element['ID']]['NAME']."<br>";?>                
                    <label for="<? echo "offer_".$offer['ID']?>"><?echo $offer['NAME']."  "." - <span>".$element['ITEM_PRICES'][0]['PRINT_PRICE']."</span>"?></label>
                    <input type="radio" id="<? echo "offer_".$offer['ID']?>" name="offers"  value="<?echo $offer['NAME']?>"/><br>
                <?}?>

And there is a script in the same file that listens for switching between radio buttons. I get an id and I need to immediately transfer it to the current php file
<script>
    $(document).ready( function () {
       
        $('input[type=radio]').change(function() {
            // console.log(this.value);
            var id = this.id.split('_');
            var ob = {
                'id': id[1]
            }
            BX.ajax({
                    url: '/bitrix/templates/adaptive_s1/components/bitrix/catalog/catalog/bitrix/catalog.element/.default/template.php',
                    type: 'POST',
                    data:JSON.stringify(ob),
                    dataType: 'json',
                    method: 'post',
                    cache:false,
                    contentType:"application/json",
                    onsuccess:function(data){
                       console.log(data);
                    }
            });
        });
    });
    </script>

As a result, $_POST is empty, nothing was written. The request itself worked with code 200
Please tell me what I'm doing wrong. How else can such a problem be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Spartak (Web-StyleStudio), 2021-05-26
@lotoseu

jQuery.ajax()
Replace

data:JSON.stringify(ob),

On the
data: ob,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question