3
나는 워드 프레스로 아약스를 처음 배운다. 위젯을 사용하여 Ajax 기능을 구현하지만 양식을 제출 한 후 0이 표시됩니다.왜 아약스 전화 후 0이
나를 안내하고 이것이 왜 더 잘 이해할 수 있는지 설명 할 수 있습니까?
내 코드 :
나는 당신처럼 아약스 예를 만들었습니다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
재생 해 주셔서 감사합니다. 나는 아약스에 대한이 튜토리얼을 심판했고 위젯으로 시도했다. 'http : // www.makeuseof.com/tag/tutorial-ajax-wordpress' – lumos
작업 코드를 제안 해주십시오. 답이 주어지지 않으면 질문을 완전히 바꾸면 문제가없는 것 같습니다. 너가 그것을하면, 핑 소리 나고 나는 볼 것이고 가까운 투표를 철회 할 것이다. – brasofilo