I use 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
Fantastic resource, I think you will all agree, but my problem is that my Javascript does not seem to be executing a PHP script ...
When I add a breakpoint in the Chrome 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}); });
He stops at the breakpoint successfully, as one would expect. However, I just can't get send.php to actually DO something, it just won't! As you can see from the code, I used an absolute URL, but it will not work with a relative URL or just send.php in this place. I put the code in send.php that writes Success to the database so that I can verify that it is running, but obviously not.
Does anyone have any ideas?
Thanks!
Jack
EDIT: just tried this code instead of what I used earlier:
$('#rating_'+rid).load('send.php', function () {alert('Load was performed.');});
The dialog displays correctly, so I'm sure it works.
For reference PHP code send.php:
<?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 a bit messy, but I have migrated the default PHP code to my CodeIgniter libraries.