1 (edited by dom 2010-10-24 14:36:33)

Topic: Ajax rating'as

Persidarau AJAX reitingą ir susidūriau su viena problema. Kai jau esi balsavęs, ant IE kartais parodo, kad galima vėl balsuoti... Ir mėginant vėl balsuoti tas echo "already voted" nesuveikia ant IE... Gaunasi klaida, kad "already_voted
Warning: Cannot modify header information - headers already sent by (output started at". Ant kitų naršyklių viskas ok. Tas already_voted, tai susietas su javascriptu. Bandžiau keitalioti, na apskritai su AJAX mažai esu susidūręs, tai nežinau kaip ištaisyt?

PHP:

// IF JAVASCRIPT IS DISABLED

if($_GET){

    $id = escape($_GET['id']);
    $rating = (int) $_GET['rating'];
    
    // If you want people to be able to vote more than once, comment the entire if/else block block and uncomment the code below it.
    
    if($rating <= 5 && $rating >= 1){
    
        if(@mysql_fetch_assoc(mysql_query("SELECT id FROM ratings WHERE IP = '".$_SERVER['REMOTE_ADDR']."' AND rating_id = '$id'")) || isset($_COOKIE['has_voted_'.$id])){
        
            echo 'already_voted';
            
        } else {
            
            setcookie('has_voted_'.$id,$id,$expire,'/',$domain,false);
            mysql_query("INSERT INTO ratings (rating_id,rating_num,IP) VALUES ('$id','$rating','".$_SERVER['REMOTE_ADDR']."')") or die(mysql_error());
            
        }
        
        header("Location:".$_SERVER['HTTP_REFERER']."");
        die;
        
    } else {
    
        echo 'You cannot rate this more than 5 or less than 1 <a href="'.$_SERVER['HTTP_REFERER'].'">back</a>';
        
    }
}

JavaScript:

// Calculate the rating
function rate(rating,id,show5,showPerc,showVotes){

    xmlHttp = GetXmlHttpObject()
    
    if(xmlHttp == null){
        alert ("Your browser does not support AJAX!");
        return;
      }

    xmlHttp.onreadystatechange = function(){
        
    var loader = document.getElementById('loading_'+id);
    var uldiv = document.getElementById('ul_'+id);
    
        if (xmlHttp.readyState == 4){ 
            
            //loader.style.display = 'none';
            var res = xmlHttp.responseText;
            
            //alert(res);
            
            if(res == 'already_voted'){
                
                loader.style.display = 'block';
                loader.innerHTML = '<div class="voted_twice">You already voted!</div>';
                
            } else {
                
                loader.style.display = 'block';
                loader.innerHTML = '<div class="voted">Thanks for voting!</div>';

                if(show5 == true){
                    var out = document.getElementById('outOfFive_'+id);
                    var calculate = res/20;
                    out.innerHTML = Math.round(calculate*100)/100; // 3.47;
                    //out.innerHTML = Math.round((calculate*2),0)/2; // 3.5;
                } 
                
                if(showPerc == true){
                    var perc = document.getElementById('percentage_'+id);
                    //var newPerc = Math.round(Math.ceil(res/5))*5;
                    var newPerc = res;
                    perc.innerHTML = newPerc+'%';
                }
                
                else if(showPerc == false){
                    var newPerc = res;
                }
                
                if(showVotes == true){
                    var votediv = document.getElementById('showvotes_'+id).firstChild.nodeValue;
                    var splitted = votediv.split(' ');
                    var newval = parseInt(splitted[0]) + 1;
                    if(newval == 1){
                        document.getElementById('showvotes_'+id).innerHTML = newval+' Vote';
                    } else {
                        document.getElementById('showvotes_'+id).innerHTML = newval+' Votes';
                    }
                }
                
                var ulRater = document.getElementById('rater_'+id);
                ulRater.className = 'star-rating2';
                
                var all_li = ulRater.getElementsByTagName('li');
                
                // start at 1 because the first li isn't a star
                for(var i=1;i<all_li.length;i++){
                    
                    all_li[i].getElementsByTagName('a')[0].onclick = 'return false;';
                    all_li[i].getElementsByTagName('a')[0].setAttribute('href','#');
                    
                }
                
                if(navigator.appName == 'Microsoft Internet Explorer'){
                    uldiv.style.setAttribute('width',newPerc+'%'); // IE
                 } else {
                    uldiv.setAttribute('style','width:'+newPerc+'%'); // Everyone else
                 }
                
            }
        } else {
            loader.innerHTML = '<img src="images/rating_loading.gif" alt="loading" />';    
        }
    
    }
    var url = "includes/rating_process.php";
    var params = "id="+id+"&rating="+rating;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);

}

Re: Ajax rating'as

su komanda header("Location:".$_SERVER['HTTP_REFERER']."");  tu nurodai, kad butu ivykdomas redirectas.. kaip isivaizduoji echo ir sia komanda vienoje vietoje?

MongoDB Certified Developer
MongoDB Certified DBA
Zend Certified Engineer

Re: Ajax rating'as

Nu čia daug problemų, tiek su PHP tiek su JS.

Kam tas redirect'as "ajax"'e?

Re: Ajax rating'as

zygis wrote:

su komanda header("Location:".$_SERVER['HTTP_REFERER']."");  tu nurodai, kad butu ivykdomas redirectas.. kaip isivaizduoji echo ir sia komanda vienoje vietoje?

Su FF ir Opera viskas tvarkoj ir su tuo redirektu...

Hmmz, reikės ieškot alternatyvos, o šiaip ajax ratingas jūsų nuomone geras pasirinkimas?

Re: Ajax rating'as

as tik sakau kad jei iskvieti komanda echo o veliau header tada bus php error'as, nes headeriai jau issiusti. To buti neturetu.

MongoDB Certified Developer
MongoDB Certified DBA
Zend Certified Engineer