Submit form when changing checkbox

0

Where am I going wrong on my form? When changing checkbox it needs to submit the form

HTML

<form id="form_onoff" name="form_onoff" action="onoff.php" method="post">
<input type="checkbox" value="on" id="slideThree" name="slideThree" />
<label for="slideThree"></label>

JQUERY

$(function($) {
$('input[type=checkbox][name=slideThree]').change(function() {
    $("#form_onoff").submit(function() {
        $.post('inc/onoff.php?id=<? echo $_SESSION['ids']; ?>', {slideThree: slideThree}, function(resposta) {
        });
    });
});

});

Thank you in advance

    
asked by anonymous 30.11.2017 / 17:19

2 answers

1

Try to check the status of the checkbox within the change() event like this:

$('input[type="checkbox"][name="slideThree"]').change(function() {
    if ($(this).is(':checked')) {
        // Aqui vai o $.post();
    }
});

Edit:

I noticed that you are currently using $.post , that is, you are performing an AJAX request within your submit . I believe it is not right to do so.

I suggest you remove $("#form_onoff").submit() from your jQuery .

    
30.11.2017 / 17:27
1
Experimente assim:

$('input[type=checkbox][name=slideThree]').on('click', function() {
  $("#form_onoff").attr("action", "inc/onoff.php?id=<? echo $_SESSION['ids']; ?>");
  $("#form_onoff").submit();
});
    
30.11.2017 / 17:36