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.

share|improve this question
You are addressing the same server and port your page is running from (localhost:8888)? – Pekka 웃 Feb 27 '10 at 22:39
Sorry, could you elaborate a little? I don't quite understand... it's being run through MAMP on my Mac, who's access address is localhost:8888 ... – Jack W-H Feb 27 '10 at 23:35
feedback

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
It's running via MAMP, I'll have a look - thanks :) – Jack W-H Feb 27 '10 at 22:39
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 6 Thank you :) – Jack W-H Feb 28 '10 at 0:42
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.