2013-10-20 3 views
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'); 

     } 
    } 
+1

* 많은 * 가지 코드 문제가있다, 나는 것 다시 시작하는 것이 좋습니다. 여기에 나와 [wordpress.se]에서 내 대답을 검색하면 WP 용 Ajax에 대한 많은 실제 예제를 찾을 수 있습니다. 그런 다음 이들 중 하나를 작동중인 위젯 코드와 통합해야합니다. 오, 그래, 이것을 구현하려면 플러그인을해야한다. – brasofilo

+0

재생 해 주셔서 감사합니다. 나는 아약스에 대한이 튜토리얼을 심판했고 위젯으로 시도했다. 'http : // www.makeuseof.com/tag/tutorial-ajax-wordpress' – lumos

+0

작업 코드를 제안 해주십시오. 답이 주어지지 않으면 질문을 완전히 바꾸면 문제가없는 것 같습니다. 너가 그것을하면, 핑 소리 나고 나는 볼 것이고 가까운 투표를 철회 할 것이다. – brasofilo

답변

3

, 그것은 당신을 도울 것입니다 수 있습니다.

먼저 플러그인을 작성하고이 코드를 추가합니다 :

이 시도

<?php 
    function sampleHelloWorld() { 
    ?> 
     <form type="post" action="" id="newCustomerForm"> 
      <label for="name">Name:</label> 
      <input name="name" type="text" /><br /><br /> 

      <input type="hidden" name="action" value="addCustomer"/> 
      <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 
    } 

function widget_myHelloWorld($args) { 
    extract($args); 
    echo $before_widget; 
    echo $before_title;?>Hello World Ajax<?php echo $after_title; 
    sampleHelloWorld(); 
    echo $after_widget; 
} 

function myHelloWorld_init(){ 
    register_sidebar_widget(__('Hello World'), 'widget_myHelloWorld');  
} 
add_action("plugins_loaded", "myHelloWorld_init"); 

wp_enqueue_script('jquery'); 

function addCustomer(){ 
    echo $name = $_POST['name']; 
    die(); 
} 

add_action('wp_ajax_addCustomer', 'addCustomer'); 
add_action('wp_ajax_nopriv_addCustomer', 'addCustomer'); 
?> 
+0

무엇이 문제의 원인입니까? 너는 무엇을 바꾸 었는가? – showdev