0

jQuery

$("input[type=checkbox]:checked").live("click", function (e) {
    var data = $('.variation:checked').map(function (i, n) {
        return {
            'val': $(n).val()
        };
    }).get(); 

    $.post('/variations.php', {
        'data': data
    }, function (data) {
        $('#variations').html(data);
    });

});

HTML output

<div id="variations">
Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [val] => 1
                )
       )
)
</div>

PHP

print_r($_POST)

Question: How do I remove value from POST if checkbox is unchecked?

user1286499
  • 227
  • 2
  • 13
  • FYI, `.live()` is deprecated. For your actual problem, just delete the value from the object of data before you send it. – Brad Jan 15 '13 at 05:27
  • 1
    There is no unchecked values in that post, 1 is `true` and 0 is `false` in this case, and it sent you an object with a true value. the logic is flawed here. – Ohgodwhy Jan 15 '13 at 05:32

2 Answers2

3

Sending form data with $('form#myForm').serialize() it will just send the checkboxes which are checked

//example 1
    $('input#submitButton').click( function() {
        $.post( 'some-url', $('form#myForm').serialize(), function(data) {
             ... do something with response from server
           },
           'json' // I expect a JSON response
        );
    });
//example 2
    $('input#submitButton').click( function() {
        $.ajax({
            url: 'some-url',
            type: 'post',
         //   dataType: 'json',
            data: $('form#myForm').serialize(),
            success: function(data) {
                       ... do something with the data...
                     }
        });
    });

from here

Community
  • 1
  • 1
Bhavin Rana
  • 1,554
  • 4
  • 20
  • 40
2

you can use .disable method to disable object and then when posting is finished enable them.or you can use method .click function when submit button clicked or if you want to dont use this values in your php you can unset($_POST['one certain key']) with unset function

HiDd3N
  • 494
  • 6
  • 23