M
M
marq2021-11-17 13:34:54
AJAX
marq, 2021-11-17 13:34:54

How to send radio value by ajax?

Good afternoon, I can not figure out how to send the radio value by ajax

<input id="radio" type="radio"  name="radio" value="1" /> Первое    
<input id="radio" type="radio" name="radio" value="2" /> Второе
<div id="aaa"></div>

$(document).ready(function () 
{
    $('#radio').on('change', function () 
    {  
            var selectedValue = $('#radio').val();
            $.ajax
            ({
                url:  "forms.php",
                type: 'POST',
                data: 'espace_id=' + selectedValue,
                success: function(data) 
                {
                   	$('#aaa').html(selectedValue);
                }
            });
        
    });
});

forms.php:
<?php
  var_dump($_POST['espace_id']);
    exit;
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex_mos, 2021-11-17
@Alex_mos

You have two identical id, this should not be.
Get the value by name, not by id. https://coderoad.ru/43188588/%D0%9F%D0%BE%D0%BB%D1...
$("input[name='radio']:checked").val();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question