0


the task is actually quite simple I believe. I want to query the status of a checkbox, pass it with $.post and JSON to a function, and then save the value.

my-html (relevant part)

<input type="checkbox" id="<?php echo $category->column_handle; ?>" name="<?php echo $category->column_handle; ?>" class="checkbox" <?php if($my_notifications->{$category->column_handle}){echo 'checked="checked"';} ?> value="1">

my-script

$('.checkbox').change(function(e){
    var parent = $(this).parents('.container');
    $.post({
        dataType: 'json',
        url: ajax_object.ajaxurl,
        data:{
            'action': 'myaction',
            'column': $(this).attr('id'),
            'check': $(this).prop('checked')
            }
    }).done(function(data){
        parent.find('.title').text(data.input);
    });
});

my-function

function my_function(){
    global $wpdb;

    $column = esc_attr($_POST['column']);
    $bool = esc_attr($_POST['check']);
    /*$id is correct and sourced elsewhere (I deleted that part)*/
    $wpdb->update('my_dataset',array($column => $bool),array('ID' => $id));

    echo json_encode(array('input' => $bool));

    die();
}

add_action('wp_ajax_myaction','my_function');

the .done part is for testing and returns the status of the checkbox correctly. But it is not saved. I have already tried a lot, such as treating the status as a string or passing 0 and 1. Everything without success. Maybe one of you can help me.

  • 1
    Try using this: update_post_meta( $post_id = post_id, $key = 'key', $value = 'value' ); – mysticalghoul Feb 1 '18 at 9:23
  • 1
    It doesn't look like OP is dealing with post meta. – Jacob Peattie Feb 1 '18 at 9:42
  • Jacob is right. I use my own record so I can not use update_post_meta. – Sin Feb 1 '18 at 9:53
  • Then may be this: $wpdb->query($wpdb->prepare("UPDATE 'my_dataset' SET $column=$bool WHERE ID=$id")); – mysticalghoul Feb 1 '18 at 10:09
  • Doesn't work either. – Sin Feb 1 '18 at 10:38
1
.prop()

method return boolean type. True or false. You should send checked value and save it into database. And in HTML get your value and set it to your checkbox. An example you have <input type="checkbox" name="test" class="checkbox" value="checked">. You should send to php $('.checkbox').val(); and then place checked attr to html.

<?php $checked = 'your var from database'; ?>
<input type="checkbox" <?php checked('checked', $checked); ?> name="test" class="checkbox" value="checked">

Something like this should work.

  • I'm already using value to get the column name, where the value should be updated. – Sin Feb 1 '18 at 9:57
  • 2
    Use update_post_meta() for saving checkbox status. Don`t use custom query to db. Or if your checkbox global, use update_option() function. – Maxim Sarandi Feb 1 '18 at 10:21
0

I solved the problem.

I tried so many things that I seemingly skipped a variant that worked.

So for anyone coming by this way it works:

my-script as in the question:

'check': $(this).prop('checked')

my-function as follows:

function my_function(){
global $wpdb;

$column = esc_attr($_POST['column']);
$bool = esc_attr($_POST['check']);
if($bool == 'true'){$value = true;}else{$value = false;}
$wpdb->update('my_dataset',array($column => $value),array('ID' => $id));

echo json_encode(array('input' => $bool));

die();
}

add_action('wp_ajax_myaction','my_function');

$bool has to be treated like a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.