2017-07-11 31 views
0

我正試圖在wordpress後端實施星級評分。我已經成功實施了這個系統。現在,我試圖將星級評分添加到數據庫,而使用admin-ajax單擊這些星星。Admin-Ajax調用不工作來實現星級評分

這是我的html:

<li name="star" value="one" onmouseover="highlightStar(this);" onmouseout="removeHighlight();" onclick="addRating(this);"> 

鼠標點擊它調用addrating在那裏我已經寫了我的AJAX功能,這是我的Ajax代碼:

function addRating(obj) { 
    $('ul.rating li').each(function(index) {alert("hello"); 
     $(this).addClass('selected'); 
     var rate=$('#rating').val((index+1)); 
     var postid=$(this).data('postid'); 
     if(index == $("ul.rating li").index(obj)) { 
      return false; 
     } 
     $.ajax({ url:"<?php echo site_url().'/wp-content/themes/options-framework-theme-master/register_ajax.php';?>", 
       type:'post', 
       data:{ 
       postid:'postid', 
       rate:'rate'}, 
       success: function(result){ 
        alert(result); 
       } 
      } 
      }); 
    }); 
} 

,這在Ajax調用去:

<?php 

    $id = $post['postid']; 

    update_post_meta($post_id,'starrating',$_POST['rating']);  


?> 

馬問題是你在點擊星星時沒有ajax電話會去。請幫助我,我k印度新阿賈克斯。

+0

什麼是'update_post_meta'($ post_id,'starrating',$ _ POST ['rating']);' 只需'echo'那裏你想要什麼作爲迴應 –

+0

更新函數更新在後端選擇了多少顆星星並轉換它轉換爲int值並將其存儲在「starrating」 – Jackson

+0

中,您需要在主題的functions.php上創建自定義ajax_action: https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action) – Bazaim

回答

1

要添加AJAX到WordPress,你可以按照下列步驟操作:

步驟1:前端(PHP文件)的外觀

<li name="star" value="one" 
          onmouseover="highlightStar(this);" 
          onmouseout="removeHighlight();" 
          onclick="addRating(this);"> 

第2步:在JavaScript(驗證和AJAX呼叫):

function addRating(obj) { 
    $('ul.rating li').each(function(index) {alert("hello"); 
     $(this).addClass('selected'); 
     var rate=$('#rating').val((index+1)); 
     var postid=$(this).data('postid'); 
     if(index == $("ul.rating li").index(obj)) { 
      return false; 
     } 
     var url = "/wp-admin/admin-ajax.php"; 
     $.ajax({ url:url, 
       type:'post', 
       data:{ 
       postid:'postid', 
       rate:'rate'}, 
       success: function(result){ 
        alert(result); 
       } 
      } 
      }); 
    }); 
} 

步驟3:鉤 一個例子:

add_action('wp_ajax_my_function_name', 'my_function_name');// for those connected to your app 
add_action('wp_ajax_nopriv_my_function_name', 'my_function_name');// for everybody 

步驟4:編寫的實際函數來處理後端邏輯(在functions.php的或類似的)

function my_function_name(){ 
    // my logic goes here 
} 

好運!