2016-11-10 28 views
0

这是我在我的网站上的投票功能。当用户点击一个按钮时,他们的投票将被插入到WordPress的自定义表格中。 AJAX返回成功消息OK,但没有任何内容被插入到表中。我在哪里犯错误?这里是我的代码(AJAX脚本和PHP代码都在一个页面上)将AJAX/javascript值插入到自定义WordPress表中

<script type="text/javascript" > 
     jQuery(document).ready(function(){ 
       var counter; 
       var id; 
      jQuery('.fa-plus').one('click', function(){ 
       counter = 0; 
       id = jQuery(this).closest('div').prop("id"); 
       alert(id); 
       //window.open("bride-profile.php"); 
       counter = counter+1; 
       jQuery('#votes-count').parent().html(counter); 

     }); 



     jQuery.ajax({ 
      url :"http://localhost/-wiz/wordpress/", 
      method :"POST", 
      data :{ 
         'action' : 'add_votes', 
         'counter': counter, 
         'id'  : id, 
      }, 
      success:function(data){ 
       alert(data);    
      } 
     }).error(function(){ 
      alert("ERROR!"); 
    }); 
}); 
    </script> <?php 
}//end of function my_action_javascript 
function add_votes(){ 
     $id = $_POST['id']; 
     $votes= $_POST['counter']; 
     $wpdb->insert(
      'fwwp_votes', 
      array(
       'bride_id' => $id, 
       'votes' => $votes 
      ) 
     ); 
    } 
add_action('wp_ajax_no_priv_add_votes', 'add_votes'); 
add_action('wp_ajax_add_votes', 'add_votes'); 

,有点我加价的:

foreach($applications as $application){ 
      $id = $application->id; 
      echo  
      '<div class="col-md-3" id="'.$id.'">', 
        '<span class="pull-right votes" id="votes-count"><strong>0</strong></span>', 
        '<div class="disp" id="disp"><i class="fa fa-heart fa-fw"></i> Votes</div>', 
        '<i class="fa fa-plus fa-fw"></i> Vote for '.$application ->user_name.', 
      '</div>'; 
      } 
+1

您必须echo'success or anything'; die();在ajx php函数的结尾@PamelaSillah –

+0

好的,我做到了,没有任何东西在页面上回显。 –

回答

0

好了,事实证明一切,我不得不做是插入我的jQuery.ajax块到onclick函数,如下所示:

jQuery(document).ready(function(){ 
       jQuery('.site-footer').hide(); 
       var counter; 
       var id; 
     jQuery('.fa-plus').one('click', function(){ 
       counter = 70; 
       id  = jQuery(this).closest('div').prop("id"); 
       alert(id); 
       counter = counter+1; 
       jQuery('#votes-count').parent().html(counter); 


       jQuery.ajax({ 
      url : "http://localhost/-wiz/wordpress/vote", 
      type : "POST", 
      data : { 
         'action' : 'add_votes', 
         'counter': counter, 
         'id'  : id 
      }, 
      success:function(response){ 
       console.log(response); 

      } 
     }).error(function(){ 
      alert("ERROR!"); 
    }); 
相关问题