Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php][js]Nie mogę uruchomić skryptu głosowania
Forum PHP.pl > Forum > Gotowe rozwiązania
Arek00
chciałem uruchomić u siebie ten skrypt Unobtrusive AJAX Star Rating Bar

skrypt przerobiłem żeby działał na cookie ale mam problem gdyż głosowanie poprzez javascript mi nie działa. wywołuję je poprzez wstawienie w odnośnik java script:sndReq('1','4'). jedyne co się dzieje to pojawia się animacja podczas której niby zapisywane są wyniki i potem powinny pokazać się już zaktualizowane wyniki jednak animacja jest cały czas

javascript do tego wygląda tak:

  1. var xmlhttp
  2.     /*@cc_on @*/
  3.     /*@if (@_jscript_version >= 5)
  4.       try {
  5.       xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
  6.      } catch (e) {
  7.       try {
  8.         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  9.       } catch (E) {
  10.        xmlhttp=false
  11.       }
  12.      }
  13.     @else
  14.      xmlhttp=false
  15.     @end @*/
  16.     if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  17.      try {
  18.       xmlhttp = new XMLHttpRequest();
  19.      } catch (e) {
  20.       xmlhttp=false
  21.      }
  22.     }
  23.     function myXMLHttpRequest() {
  24.       var xmlhttplocal;
  25.       try {
  26.         xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
  27.      } catch (e) {
  28.       try {
  29.         xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
  30.       } catch (E) {
  31.         xmlhttplocal=false;
  32.       }
  33.      }
  34.  
  35.     if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
  36.      try {
  37.       var xmlhttplocal = new XMLHttpRequest();
  38.      } catch (e) {
  39.       var xmlhttplocal=false;
  40.       alert('couldn't create xmlhttp object');
  41.      }
  42.     }
  43.     return(xmlhttplocal);
  44. }
  45.  
  46. function sndReq(vote,id_num) {
  47.     var theUL = document.getElementById('unit_ul'+id_num); // the UL
  48.    
  49.     // switch UL with a loading div
  50.     theUL.innerHTML = '<div class="loading"></div>';
  51.    
  52.    xmlhttp.open('get', 'rpc.php?j='+vote+'&q='+id_num);
  53.    xmlhttp.onreadystatechange = handleResponse;
  54.    xmlhttp.send(null);    
  55. }
  56.  
  57. function handleResponse() {
  58.  if(xmlhttp.readyState == 4){
  59.          if (xmlhttp.status == 200){
  60.            
  61.        var response = xmlhttp.responseText;
  62.        var update = new Array();
  63.  
  64.        if(response.indexOf('|') != -1) {
  65.            update = response.split('|');
  66.            changeText(update[0], update[1]);
  67.        }
  68.          }
  69.    }
  70. }
  71.  
  72. function changeText( div2show, text ) {
  73.    // Detect Browser
  74.    var IE = (document.all) ? 1 : 0;
  75.    var DOM = 0;
  76.    if (parseInt(navigator.appVersion) >=5) {DOM=1};
  77.  
  78.    // Grab the content from the requested "div" and show it in the "container"
  79.    if (DOM) {
  80.        var viewer = document.getElementById(div2show);
  81.        viewer.innerHTML = text;
  82.    }  else if(IE) {
  83.        document.all[div2show].innerHTML = text;
  84.    }
  85. }
  86.  
  87. /* =============================================================== */
  88. var ratingAction = {
  89.          'a.rater' : function(element){
  90.               element.onclick = function(){
  91.  
  92.               var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
  93.               var parameterTokens = parameterString.split("&"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
  94.               var parameterList = new Array();
  95.  
  96.               for (j = 0; j < parameterTokens.length; j++) {
  97.                    var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
  98.                    var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
  99.                    parameterList[parameterName] = parameterValue;
  100.               }
  101.               var theratingID = parameterList['q'];
  102.               var theVote = parameterList['j'];
  103.               var theuserIP = parameterList['t'];
  104.               var theunits = parameterList['c'];
  105.              
  106.               //for testing     alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
  107.               sndReq(theVote,theratingID,theuserIP,theunits); return false;          
  108.               }
  109.          }
  110.          
  111.     };
  112. Behaviour.register(ratingAction);


plik rpc.php:
  1. <?php
  2. //getting the values
  3. $vote_sent = $_REQUEST['j'];
  4. $id_sent = $_REQUEST['q'];
  5.  
  6. //connecting to the database to get some information
  7. $query = mysql_query("SELECT ilosc_glosow, suma_glosow, srednia_glosow FROM ratings WHERE id_prod
    uktu='$id_sent' "
    );
  8. $numbers = mysql_fetch_assoc($query);
  9. $numbers = mysql_fetch_assoc($query);
  10. $ilosc_glosow = $numbers['ilosc_glosow']; //how many votes total
  11. $suma_glosow = $numbers['suma_glosow']; //total number of rating added together and stored
  12. $suma_glosow = $suma_glosow + $vote_sent; // add together the current vote value and the total vote value
  13. $tense = ($count==1) ? "vote" : "votes"; //plural form votes/vote
  14. $nowa_ilosc_glosow = $ilosc_glosow + 1;
  15. $srednia_glosow = $suma_glosow/$nowa_ilosc_glosow;
  16.  
  17. $tablica = array();
  18. if (isset($_COOKIE['ratings'])) {
  19. $tablica = unserialize($_COOKIE['ratings']);
  20. }
  21.  
  22. if ($ilosc_glosow == 0) {
  23.  mysql_query("INSERT INTO ratings SET id_produktu='$id_sent', ilosc_glosow='1', suma_glosow='$vote_sent', srednia_glosow='$vote_sent'");
  24. } else {
  25.  mysql_query("UPDATE ratings SET ilosc_glosow='$nowa_ilosc_glosow', suma_glosow='$suma_glosow', srednia_glosow='$srednia_glosow' WHERE id_produktu='$id_sent'");
  26. }
  27.  array_push($tablica,$id_sent);
  28. //  setcookie('ratings', serialize($tablica), time()+3600);
  29.  
  30. // these are new queries to get the new values!
  31. $newtotals = mysql_query("SELECT ilosc_glosow, suma_glosow, srednia_glosow FROM ratings WHERE id='$id_sent'");
  32. $numbers = mysql_fetch_assoc($newtotals);
  33. $count = $numbers['ilosc_glosow'];//how many votes total
  34. $current_rating = $numbers['suma_glosow'];//total number of rating added together and stored
  35. $tense = ($count==1) ? "vote" : "votes"; //plural form votes/vote
  36.  
  37. $new_back = 
  38. "<ul class="unit-rating" style="width:100px;">n".
  39. "<li class="current-rating" style="width:". @number_format($current_rating/$count,2)*20 ."px;">Current rating.</li>n".
  40. "<li class="r1-unit">1</li>n".
  41. "<li class="r2-unit">2</li>n".
  42. "<li class="r3-unit">3</li>n".
  43. "<li class="r4-unit">4</li>n".
  44. "<li class="r5-unit">5</li>n".
  45. "<li class="r6-unit">6</li>n".
  46. "<li class="r7-unit">7</li>n".
  47. "<li class="r8-unit">8</li>n".
  48. "<li class="r9-unit">9</li>n".
  49. "<li class="r10-unit">10</li>n".
  50. "</ul>".
  51. "<p class="voted">$id_sent. Rating: <b>".@number_format($current_rating/$count,1)."</b>/5 (".$count." ".$tense." cast) ".
  52. "<span class="thanks">Thanks for voting!</span></p>";//show the updated value of the vote
  53.  
  54.  
  55. //name of the div id to be updated | the html that needs to be changed
  56. $output = "unit_long$id_sent|$new_back";
  57. echo $output;
  58. ?>


przykład tego o czym mówię: click
revyag
Przenoszę Przedszkole -> Gotowe Rozwiązania
bigus
w pliku rating.js linia 62

  1. <?php
  2. xmlhttp.open('get', 'rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units);
  3. ?>

zmień na
  1. <?php
  2. xmlhttp.open('get', '../sciezka_gdzie_to_masz/rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units);
  3. ?>


Pobierz też najnowszą wersje
chudy9
no chyba bedzie dobrze
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.