drde3456 2014-12-04 05:09
浏览 51
已采纳

wordpress ajax不更新数据库

hope you can guide me, I am trying to run this tutorial: http://www.inkthemes.com/how-to-use-ajax-in-wordpress-for-data-insertion/#

This is my Wordpress Plugin 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
            }
        });
    });
});

this is the plugin 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
?>

so, what i am finding in my debugger is that the console POST data is the 'dname' submitted in the form input. however, the database table "bio" is not being updated. all that happens is the alert pops up full of all the html of the website im working on. ie. the RESPONSE in the console debugger is a massive html text.

So, I dont understand why data=my website html AND why the table "bio" is not updating.

  • 写回答

2条回答 默认 最新

  • donglv9813 2014-12-04 05:29
    关注

    in plugin php file first add your java script file like this

    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'),
                                )
                            );
    

    Plugin Javascript: add admin-ajax.php file 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
                }
            });
        });
    });
    

    for batter under standing check this link http://codex.wordpress.org/AJAX_in_Plugins

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同