Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

i get this a lot........ajaxurl needs to be defined, so i've learned that its just easier to not use ajaxurl and put in "/wp-admin/admin-ajax.php" in the url section.

Also i dont see you using non-conflict jQuery? (use the word jQuery instead of $)

You may also have issues with your postdata string, i may be wrong but what you need is action: '' , rating: '',

etc.

A good practice is to var_dump $_POST and exit at the beginning of your function to make sure they are passing over correctly. then in success- console.log(res) or whatever you are calling your return data

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

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

                //alert(postdata1); -> console.log() is better for looking at objects 
                jQuery.ajax({
                    type:'POST',
                    url: "/wp-admin/admin-ajax.php",
                    cache:false,
                    data: {
                        action: 'my_special_ajax_call5',
                        rating: rating,
                        trackname: trackname
                    }

                    success:function(output){
                        console.log(output)
                        jQuery(cssSelector.rating_succes).html(output).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));
            });
        }

see how you get on with that :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top