3
我是第一次使用WordPress学习Ajax。我正在使用小部件实现Ajax功能,但在提交表单后我得到了0。为什么在ajax调用后为零
你能指导我解释为什么发生这种情况,所以我可以更好地理解。
我的代码:
wp_enqueue_script('jquery');
/**
* Add function to widgets_init that'll load our widget.
* @since 0.1
*/
add_action('widgets_init', 'example_load_widgets');
/**
* Register our widget.
* 'Example_Widget' is the widget class used below.
*
* @since 0.1
*/
function example_load_widgets() {
register_widget('Example_Widget');
}
/**
* Example Widget class.
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
* @since 0.1
*/
class Example_Widget extends WP_Widget {
/**
* Widget setup.
*/
function Example_Widget() {
/* Widget settings. */
$widget_ops = array('classname' => 'example', 'description' => __('An example widget that displays a person\'s name and sex.', 'example'));
/* Widget control settings. */
$control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'example-widget');
/* Create the widget. */
$this->WP_Widget('example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops);
}
/**
* How to display the widget on the screen.
*/
function widget() {
echo $name = $_POST['name'];
?>
<form type="post" action="" id="newCustomerForm">
<label for="name">Name:</label>
<input name="name" type="text" />
<input type="hidden" name="action" value="example_load_widgets"/>
<input type="submit" name="submit" value="Submit">
</form>
<br/><br/>
<div id="feedback"></div>
<br/><br/>
<script type="text/javascript">
jQuery('#newCustomerForm').submit(ajaxSubmit);
function ajaxSubmit(){
var newCustomerForm = jQuery(this).serialize();
jQuery.ajax({
type:"POST",
url: "http://localhost/testing/wordpress/wp-admin/admin-ajax.php",
data: newCustomerForm,
success:function(data){
jQuery("#feedback").html(data);
},
error: function(errorThrown){
alert(errorThrown);
}
});
return false;
}
</script>
<?php
die();
add_action('wp_ajax_example_load_widgets', 'example_load_widgets');
add_action('wp_ajax_nopriv_example_load_widgets', 'example_load_widgets');
}
}
有很多* *的东西你的代码错误,我建议你重新开始。如果你在这里和[wordpress.se]搜索我的答案,你会发现许多WP的Ajax工作示例。然后,您必须将其中一个与正在运行的窗口小部件代码集成在一起。哦,是的,你应该做一个插件来实现这一点。 – brasofilo
感谢您的重播并指导我。我已经参考了Ajax的这个教程,并尝试了一些小部件。 'http:// www.makeuseof.com/tag/tutorial-ajax-wordpress' – lumos
我建议你拿出一些工作代码。如果没有给出答案,我认为完全改变问题是没问题的。如果你这样做,平了我,我会看看并收回近距离投票。 – brasofilo