wpdb – WordPress ajax response save into database

Question

I have this code which grab playlist from youtube and through foreach and ajax send data to wordpress function to save into database. IT is not working, i know somewhere i’m doing wrong but unable to find. followin is my code

add_action('admin_footer','add_into_playlist_function');
function add_into_playlist_function() {
    ?>
    <script src="https://apis.google.com/js/api.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function($){            
            $('.add-playlist').on('click', function(e){       
                e.preventDefault();                
                var url="https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&key=API_KEY&channelId=CHANNEL_ID&maxResults=50";
                $.getJSON(url).done(function(response){
                    //console.log(response);
                    response["items"].forEach( function( value, cxv){
                        //console.log(value.id);
                        var data = {
                            'action': 'save_playlist_data',
                            'security': '<?php echo wp_create_nonce( "wpdocs-special-string" ); ?>',
                           'playlist_code': value.id,
                           'playlist_title': value.snippet.title,
                           'playlist_image': value.snippet.thumbnails.medium.url,
                        }
                        $.ajax({
                            url: '<?php echo admin_url( "admin-ajax.php" ); ?>',
                            data: data,
                            method: 'POST',
                            dataType: 'json',
                            success ( res ) {
                                if ( ! res.success ) {
                                    console.log( res.data );
                                } else {
                                    console.log( res );
                                }
                            },
                            error ( xhr, error, status ) {
                                console.log( error, status );
                            }
                        });
                    });
                });                
            });
        });
    </script>
    <?php
}

function save_data_table() {
    global $wpdb;
    $output = ['status' => 1];
    if ( ! check_ajax_referer( 'wpdocs-special-string', 'security', false ) ) {
        wp_send_json_error( 'Invalid Request' );
    }
    
    $playlist_code = $_POST['playlist_code'];
    $playlist_title = $_POST['playlist_title'];
   $playlist_image = $_POST['playlist_image'];
    $data = array(
        'playlist_code' => $playlist_code,
        'playlist_image' => $playlist_image,
        'playlist_title' => $playlist_title,
    );
    $wpdb->insert( 'wp_youtube_playlist', $data );
    $output['status'] = 2;
    wp_send_json($output);
    
}
add_action( 'wp_ajax_save_playlist_data', 'save_data_table' );
add_action( 'wp_ajax_nopriv_save_playlist_data', 'save_data_table');

kindly correct me where i’m wrong. thanks millions

0
Abdul Muhaymin 1 month 2022-08-22T17:39:13-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse