2016-11-10 6 views
0

これは私のサイトにある投票機能です。ユーザーがボタンをクリックすると、投票がWordPressのカスタムテーブルに挿入されます。 AJAXは成功メッセージOKを返しますが、テーブルに何も挿入されていません。私はどこでミスをしていますか?ここに私のコードは(AJAXスクリプトとPHPのコードが1つのページにすべてです)カスタムWordPressテーブルにAJAX/javascriptの値を挿入する

<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

「成功か何か」をエコーする必要があります; @ pamelaSillahの最後の@PamelaSillah –

+0

OK私はそれをして、何もページにエコーされていません。 –

答えて

0

OK、それは私が持っていたすべてが判明ので、私の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!"); 
    }); 
関連する問題