Php wordpress ajax不更新数据库

Php wordpress ajax不更新数据库,php,ajax,wordpress,insert,Php,Ajax,Wordpress,Insert,希望您能指导我,我正在尝试运行此教程: 这是我的Wordpress插件Javascript: jQuery(document).ready(function(){ jQuery("#submit").click(function(){ console.log("click caught");//this bit works var name = jQuery("#dname").val(); jQuery.ajax({

希望您能指导我,我正在尝试运行此教程:

这是我的Wordpress插件Javascript:

jQuery(document).ready(function(){
    jQuery("#submit").click(function(){
        console.log("click caught");//this bit works
        var name = jQuery("#dname").val();
        jQuery.ajax({
            type: 'POST',   
            data: {"action": "post_word_count", "dname":name},
            success: function(data){ 
                alert(data);//this alert appears full of html
            }
        });
    });
});
这是php的插件:

function show_form(){

echo "<form>";
echo "<label>Name</label>";
echo "<input type='text' id='dname' name='dname' value=''/><br/>";
echo "<input type='button' id='submit' name='submit' value='Submit'/>";
echo "</form>";
}
add_action('the_content', 'show_form');



function post_word_count(){

$name = $_POST['dname'];
global $wpdb;
$wpdb->insert(
    'bio',
    array(
        'bio_surname' => $name
    ),
    array(
        '%s'
    )
);

die();
return true;
}
//
add_action('wp_ajax_post_word_count', 'post_word_count'); // Call when user logged in
add_action('wp_ajax_nopriv_post_word_count', 'post_word_count'); // Call when user in not logged in
?>
因此,我在调试器中发现,控制台POST数据是表单输入中提交的“dname”。但是,没有更新数据库表bio。所发生的一切就是弹出一个警报,里面充满了我正在处理的网站的所有html。控制台调试器中的响应是大量html文本


所以,我不明白为什么data=my-website-html以及为什么表bio没有更新。

在plugin-php文件中,首先像这样添加java脚本文件

wp_enqueue_script('ajax-script-xyz','***javascript file path***', array('jquery')); 
wp_localize_script('ajax-script-xyz', 'ajax_object',
                        array(
                            'ajax_url' => admin_url('admin-ajax.php'),
                            )
                        );
插件Javascript:添加admin-ajax.php文件url

jQuery(document).ready(function(){
    jQuery("#submit").click(function(){
        console.log("click caught");//this bit works
        var name = jQuery("#dname").val();
        jQuery.ajax(ajax_object.ajax_url, {//**add admin-ajax.php fill url **
            type: 'POST',   
            data: {"action": "post_word_count", "dname":name},
            success: function(data){ 
                alert(data);//this alert appears full of html
            }
        });
    });
});

对于batter Understanding,请检查此链接

您尚未在ajax中定义URL:

jQuery(document).ready(function(){
    jQuery("#submit").click(function(){
        var name = jQuery("#dname").val();
        jQuery.ajax({
            type: 'POST',   // Adding Post method
             url: ajaxurl, // Including ajax file
             data: {"action": "post_word_count", "dname":name}, // Sending data dname to post_word_count function.
             success: function(data){ // Show returned data using the function.
                 alert(data);
        });
    });
});