【问题标题】:wordpress ajax not updating databasewordpress ajax不更新数据库
【发布时间】:2014-12-04 13:34:49
【问题描述】:

希望您能指导我,我正在尝试运行本教程: http://www.inkthemes.com/how-to-use-ajax-in-wordpress-for-data-insertion/#

这是我的 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。控制台调试器中的响应是大量的 html 文本。

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

【问题讨论】:

    标签: php ajax wordpress insert


    【解决方案1】:

    在插件 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
                }
            });
        });
    });
    

    对于站立状态下的击球手,请查看此链接http://codex.wordpress.org/AJAX_in_Plugins

    【讨论】:

      【解决方案2】:

      你还没有在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);
              });
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-20
        • 1970-01-01
        • 1970-01-01
        • 2019-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多