duankezong4064 2014-04-16 10:31
浏览 42
已采纳

评级不会在wordpress中添加ajax

I have a problem There is a rating system on songs (Its not my code i debugging it). but it could not add or update or show me the rating.

here is my code: Ajax.js

function bindEvents() {
                $(cssSelector.rating_succes).css('display','none');
                //Set the new rating when the user clicks
                $(cssSelector.ratingLevel).click(function() {
                    var $this = $(this), rating = $this.parent().children().index($this) + 1, index;

                    var trackname   =   $(cssSelector.title+':first').text();

                    var postdata1   =   'action=my_special_ajax_call5&rating='+rating+'&trackname='+trackname;  
                    alert(postdata1);
                    jQuery.ajax({
                        type:'POST',
                        url:ajaxurl,
                        cache:false,
                        data: postdata1,
                        beforeSend:function(){

                        },
                        success:function(res){
                            $(cssSelector.rating_succes).html(res).fadeIn(500).delay(1000).fadeOut(500);
                            //window.setTimeout(function(){location.reload()},2000);
                        }
                });
                    $this.prevAll().add($this).addClass(attr(cssSelector.ratingLevelOn)).end().end().nextAll().removeClass(attr(cssSelector.ratingLevelOn));
                });
            }

Proccess.php

function implement_ajax5(){

global $wpdb;
$table = $wpdb->prefix."songs";
$table1 = $wpdb->prefix."rating";

$song_title = strip_tags($_POST['trackname']);
$rating_value = strip_tags($_POST['rating']);

$songres = $wpdb->get_row("SELECT * FROM $table WHERE `title`='$song_title'") or die(mysql_error());
$song_id = $songres->id;
$total_votes = $songres->total_votes;
$total_votes = $total_votes+1;

$ip = $_SERVER['REMOTE_ADDR'];


$data = array(
    'song_id' => $song_id,
    'rating_value' => $rating_value,
    'user_ip' => $ip
);


$check = $wpdb->get_results("SELECT * FROM $table1 WHERE song_id='$song_id' AND user_ip='$ip'");
if(!$check){

$insert = $wpdb->insert($table1,$data);


$wpdb->update( 
    $table, 
    array(
        'total_votes' => $total_votes,
    ), 
    array( 'ID' => $song_id ) 

) or die(mysql_error());
echo 'Thank you';
}else{echo 'Already rated';}    

die();

}

index.php

add_action('wp_ajax_my_special_ajax_call5', 'implement_ajax5');
add_action('wp_ajax_nopriv_my_special_ajax_call5', 'implement_ajax5');//for users that are not logged in.

I dont understand what happen when i alert it shows me right values but not add or update in database.

  • 写回答

2条回答 默认 最新

  • douzhang6176 2014-04-17 10:40
    关注

    ok just try this in your Ajax.js at top of the page

    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
    

    And every thing goes perfect

    and i think in your process page there is no need to update query. If you want to delete this there is no issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了