views:

78

answers:

1

Hi y'all!

I'm using the source code found on this site here: http://webtint.net/tutorials/5-star-rating-system-in-php-mysql-and-jquery/comment-page-1/#comment-2562

A fantastic resource, I think you'll all agree, but my problem is that my Javascript doesn't appear to be actually executing the PHP script...

When I add a breakpoint in Chrome's debugger for the penultimate line (before }); ):

$("[id^=rating_]").children("[class^=star_]").click(function() {

        var current_star = $(this).attr("class").split("_")[1];
        var rid = $(this).parent().attr("id").split("_")[1];

        $('#rating_'+rid).load('http://localhost:8888/fivestars/send.php', {rating: current_star, id: rid});


    });

It stops on the breakpoint successfully, as you'd expect. However I just can't get send.php to actually DO anything, it simply won't! As you can see from the code I've used an absolute URL but it won't work with relative URL or just send.php in that place. I've placed code in send.php that writes 'Success' to the database just so I can test that it's being executed - but clearly isn't.

Does anyone have any ideas?

Thanks!

Jack

EDIT: Just tried this code in place of what I was previously using:

$('#rating_'+rid).load('send.php', function () {alert('Load was performed.');});

The dialogue displays correctly, so I'm confident it's working.

For reference, the PHP code of send.php is:

<?php

$rating = (int)$_POST['rating'];
$id = (int)$_POST['id'];

$this->db->query("INSERT INTO code (rating) VALUE (8)");
$query = $this->db->query("SELECT * FROM code WHERE id = '".$id."'");

while($query->row()) {

    if($rating > 5 || $rating < 1) {
        echo"Rating can't be below 1 or more than 5";
    }

    elseif(isset($_COOKIE['rated'.$id])) {
        echo"<div class='highlight'>You've already voted, sorry!</div>";
    }
    else {
        setcookie("rated".$id, $id, time()+60*60*24*365);

//      $total_ratings = $row['total_ratings'];
        $total_ratings = $query->row('total_ratings');
        $total_rating = $query->row('total_rating');
        $current_rating = $query->row('rating');

        $new_total_rating = $total_rating + $rating;
        $new_total_ratings = $total_ratings + 1;
        $new_rating = $new_total_rating / $new_total_ratings;
        // Lets run the queries. 

        $this->db->query("UPDATE test SET total_rating = '".$new_total_rating."' WHERE id = '".$id."'");
        $this->db->query("UPDATE test SET rating = '".$new_rating."' WHERE id = '".$id."'");
        $this->db->query("UPDATE test SET total_ratings = '".$new_total_ratings."' WHERE id = '".$id."'");

        echo"<div class='highlight'>Thanks for your vote!</div>";

    }
}

?>

Sorry that's a little messy, but I've been migrating default PHP code to my CodeIgniter Libraries.

+2  A: 

First thing to check would the the access and error logs from the web server to see if the request is actually getting to the web server.

Richard Harrison
It's running via MAMP, I'll have a look - thanks :)
Jack Webb-Heller
Logs to the rescue![27-Feb-2010 22:25:05] PHP Fatal error: Using $this when not in object context in /Users/Jack/Sites/howCode.com/fivestars/send.php on line 6Thank you :)
Jack Webb-Heller