【发布时间】: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”没有更新。
【问题讨论】: