wordpress ajax不更新数据库 [英] wordpress ajax not updating database

查看:110
本文介绍了wordpress ajax不更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望您能指导我,我正在尝试运行此教程: http://www.inkthemes.com /如何在wordpress中使用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都充满了. IE.控制台调试器中的RESPONSE是大量HTML文本.

所以,我不明白为什么data =我的网站html以及为什么表"bio"没有更新.

解决方案

在插件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文件网址

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
            }
        });
    });
});

常备击球手,请检查此链接 http://codex.wordpress.org/AJAX_in_Plugins

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.

解决方案

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

这篇关于wordpress ajax不更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆