开发者

JQuery and PHP rate me script?

开发者 https://www.devze.com 2022-12-15 16:29 出处:网络
I found this rating script on [http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax] and I cant seem to get it to work like in the example on http://jquery.bassistance.de/e

I found this rating script on [http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax] and I cant seem to get it to work like in the example on http://jquery.bassistance.de/example-rateme.html when I click the rating nothing happens.

How can I fix this problem?

Here is the complete script below.

<!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");
    fw开发者_开发知识库rite($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>


o Check jsquery. The jquery question is valid--make sure you are pointing to a valid jquery source file (for me, it is "jquery-1.4.2.js" in the current directory.

o Check the location of rate.php. Example-rateme.html expects to find rate.php in folder starterkit. The files as shipped are all in one folder--so, fix this.

o php sessions must be turned on. The default php I installed on my test server on my pc uses an ini file with the session.auto_start flag turned off (http://www.learnphp-tutorial.com/Sessions.cfm).

o Restart your server, flush caches and sacrifice a small animal. I'm joking here a little, but I did spend a lot of time chasing problems that vanished after a while, because, I suspect, I was running the old version of file(s) that I thought were updated.


Use Firebug to get/see any javascript errors... You could also check the error console in Firefox. Without knowing what's happening (or not), it's hard to fix the problem.

Silly question, but do you have jquery.js on the server?

0

精彩评论

暂无评论...
验证码 换一张
取 消