2010-01-05 33 views
0

我在[http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax]上發現了此評級腳本,我無法像http://jquery.bassistance.de/example-rateme.html上的示例那樣使用它,當我點擊評分時什麼也沒有發生。JQuery和PHP評分我的腳本?

我該如何解決這個問題?


這裏是下面的完整的腳本。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>jQuery Starterkit</title> 

<link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" /> 
<script src="jquery.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    // generate markup 
    var ratingMarkup = ["Please rate: "]; 
    for(var i=1; i <= 5; i++) { 
     ratingMarkup[ratingMarkup.length] = "<a href='#'>" + i + "</a> "; 
    } 
    var container = $("#rating"); 
    // add markup to container 
    container.html(ratingMarkup.join('')); 

    // add click handlers 
    container.find("a").click(function(e) { 
     e.preventDefault(); 
     // send requests 
     $.post("starterkit/rate.php", {rating: $(this).html()}, function(xml) { 
      // format result 
      var result = [ 
       "Thanks for rating, current average: ", 
       $("average", xml).text(), 
       ", number of votes: ", 
       $("count", xml).text() 
      ]; 
      // output result 
      $("#rating").html(result.join('')); 
     }); 
    }); 
}); 
</script> 

</head> 
<body> 
<h1>jQuery Getting Started Example - rate me</h1> 

<?php 

define('STORE', 'ratings.dat'); 

function put_contents($file,$content) { 
    $f = fopen($file,"w"); 
    fwrite($f,$content); 
    fclose($f); 
} 

if(isset($_REQUEST["rating"])) { 
    $rating = $_REQUEST["rating"]; 
    $storedRatings = unserialize(file_get_contents(STORE)); 
    $storedRatings[] = $rating; 
    put_contents(STORE, serialize($storedRatings)); 
    $average = round(array_sum($storedRatings)/count($storedRatings), 2); 
    $count = count($storedRatings); 
    $xml = "<ratings><average>$average</average><count>$count</count></ratings>"; 
    header('Content-type: text/xml'); 
    echo $xml; 
} 

?> 

<p>This example demonstrate basic use of AJAX. Click one of the links below to rate. The 
number of rating and the average rating will be returned from the serverside script and displayed.</p> 

<div id="rating">Container</div> 

</body> 
</html> 

回答

1

使用Firebug獲取/查看任何javascript錯誤...您也可以在Firefox中檢查錯誤控制檯。如果不知道發生了什麼(不),很難解決問題。

愚蠢的問題,但你有jquery.js在服務器上?

2

o 檢查jsquery。 jQuery的問題是有效的 - 確保你在當前目錄指向一個有效的jQuery源文件(對我來說,它是「jQuery的1.4.2.js」

Ø檢查率的位置。 。.PHP例,rateme.html希望能夠找到文件夾中STARTERKIT rate.php作爲附帶的文件都是一個文件夾中 - 。所以,解決這個問題

ØPHP會話必須打開的。默認php我安裝在我的電腦上的測試服務器上,使用ini文件,並關閉session.auto_start標誌(http://www.learnphp-tutorial.com/Sessions.cfm)。

o 重新啓動服務器,刷新緩存並犧牲一隻小動物。我在這裏有點開玩笑,但我確實花了很多時間來追逐那些在一段時間後消失的問題,因爲我懷疑我正在運行我認爲已更新的舊版本文件。