Data insertion after clicking a button does not work (script, AJAX, PHP)

1 - the button "Choice" has Id "ButtonPubVideoHdp" in Elementor2 - the file wp-contentthemesgener

1 - the button "Choice" has Id "ButtonPubVideoHdp" in Elementor

2 - the file wp-content/themes/generatepress/js/wp_ajax_calls.js manages the click and calls in the file wp-content/themes/generatepress/functions.php the'buttonpubvideohdp_insertdata'

jQuery(document).ready(function() { 
   jQuery("#ButtonPubVideoHdp").click(function () {
    console.log('The function is hooked up');
    jQuery.ajax({
        type: "POST",
        url: "/wp-content/themes/generatepress/functions.php",
        data: {
            action: 'buttonpubvideohdp_insertdata',
        },
        success: function (output) {
           console.log(output);
        }
        });
        console.log('The function is finished');
    });
});

3 - the file wp-content/themes/generatepress/functions.php asks the insert in data base via the function buttonpubvideohdp_insertdata(). The php code is inserted in the file functions.php by a plugin

wp_enqueue_script('ajax-calls', get_template_directory_uri() .'/js/wp_ajax_calls.js', array('jquery'), '1.0', true);
    
// register the ajax action for authenticated users
add_action('wp_ajax_buttonpubvideohdp_insertdata', 'buttonpubvideohdp_insertdata');

// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_buttonpubvideohdp_insertdata', 'buttonpubvideohdp_insertdata');

// handle the ajax request
function buttonpubvideohdp_insertdata() {
    global $wpdb;
    $User_Name = 'Nouveau nom 14h26';
    $table_name = $wpdb->prefix . 'Yearbook_test';
    echo "Hi everyone";

    $wpdb->insert( 
        $table_name, 
        array( 
            'User_Name' => $User_Name, 
        ) 
    );
    echo $wpdb->last_error ;
    
}

No insert, please help.

1 - the button "Choice" has Id "ButtonPubVideoHdp" in Elementor

2 - the file wp-content/themes/generatepress/js/wp_ajax_calls.js manages the click and calls in the file wp-content/themes/generatepress/functions.php the'buttonpubvideohdp_insertdata'

jQuery(document).ready(function() { 
   jQuery("#ButtonPubVideoHdp").click(function () {
    console.log('The function is hooked up');
    jQuery.ajax({
        type: "POST",
        url: "/wp-content/themes/generatepress/functions.php",
        data: {
            action: 'buttonpubvideohdp_insertdata',
        },
        success: function (output) {
           console.log(output);
        }
        });
        console.log('The function is finished');
    });
});

3 - the file wp-content/themes/generatepress/functions.php asks the insert in data base via the function buttonpubvideohdp_insertdata(). The php code is inserted in the file functions.php by a plugin

wp_enqueue_script('ajax-calls', get_template_directory_uri() .'/js/wp_ajax_calls.js', array('jquery'), '1.0', true);
    
// register the ajax action for authenticated users
add_action('wp_ajax_buttonpubvideohdp_insertdata', 'buttonpubvideohdp_insertdata');

// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_buttonpubvideohdp_insertdata', 'buttonpubvideohdp_insertdata');

// handle the ajax request
function buttonpubvideohdp_insertdata() {
    global $wpdb;
    $User_Name = 'Nouveau nom 14h26';
    $table_name = $wpdb->prefix . 'Yearbook_test';
    echo "Hi everyone";

    $wpdb->insert( 
        $table_name, 
        array( 
            'User_Name' => $User_Name, 
        ) 
    );
    echo $wpdb->last_error ;
    
}

No insert, please help.

Share Improve this question edited Dec 11, 2023 at 18:26 Jean-François Moulin asked Dec 11, 2023 at 13:45 Jean-François MoulinJean-François Moulin 11 bronze badge 4
  • Does the database table exist? – Jacob Peattie Commented Dec 11, 2023 at 13:52
  • Yes, the database exists (+table + field). When manually launched, the function buttonpubvideohdp_insertdata() works. – Jean-François Moulin Commented Dec 11, 2023 at 14:04
  • Is your console message being logged? What response is returned by the AJAX request? – Jacob Peattie Commented Dec 11, 2023 at 15:08
  • Yes the console log is The function is hooked up wp_ajax_calls.js?ver=1.0:3 The function is finished wp_ajax_calls.js?ver=1.0:14 wp_ajax_calls.js?ver=1.0:11 But no response from the AJAX request. – Jean-François Moulin Commented Dec 11, 2023 at 15:54
Add a comment  | 

1 Answer 1

Reset to default 0

Here a screenshot of the log, it is more precise.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1736720516a3908628.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信