1

I am trying to insert the values of all ticked checkboxes in a wordpress database, into a table which i created myself. Here's my jQuery code:

$(document).ready(function(){
    $('#submit').click(function(){
        var insert = [];
        $('.get_value').each(function(){
            if($(this).is(":checked")  ){
                insert.push($(this).val() );
            }
        });
        insert = insert.toString();
        $.ajax({
            url:"insert.php",
            method:"POST",
            data:{insert:insert},
            success:function(data){
                $('.result').html(data);
            }
        });

    });
});

and my insert code, (its in insert.php):

if ( isset($_POST["insert"]) ){
    global $wpdb;
    $table_name = $wpdb->prefix . "status";
    $wpdb->insert($table_name, array(
         'status' => 'approved'
    );
}

its not showing any errors, but its not inserting either.I dont know if this information is useful, but the checkboxes are at the admin backend as part of a custom post type.

1 Answer 1

1

This isn't going to work, because you are calling insert.php in your ajax call. The url should point to ajaxurl that is localized with the script where your ajax is.

So if your script is located in file called custom.js and you have it enqueued it like:

wp_enqueue_script( 'my_custom_script', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ),'', true );

Then you'll localize your ajax url like

wp_localize_script( 'my_custom_script', 'ajax_call', array(
    'ajaxurl'   => admin_url( 'admin-ajax.php' ),
) );

And use it in your script like

$(document).ready(function(){
    $('#submit').click(function(){
        var insert = [];        
        $('.get_value').each(function(){
            if($(this).is(":checked")  ){
                 insert.push($(this).val() );   
            }
        });
        insert = insert.toString();
        $.ajax({
            url: ajax_call.ajaxurl,
            method:"POST",
            data:{
                'action': 'my_action_callback',
                'insert': insert,
            },
            success:function(data){
                $('.result').html(data);
            }
        });
    });
});

Also your insert.php code should be inside a function hooked on wp_ajax hooks

add_action( 'wp_ajax_my_action_callback', 'my_action_callback' );
add_action( 'wp_ajax_nopriv_my_action_callback', 'my_action_callback' );

function my_action_callback(){
    if ( isset($_POST["insert"]) ){
        global $wpdb;
        $table_name = $wpdb->prefix . "status";
        $wpdb->insert($table_name, array(
             'status' => 'approved'
        );
    }
}

https://codex.wordpress.org/AJAX_in_Plugins

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.