开发者

jQuery, PHP: Calling a PHP script from jQuery, but it doesn't appear to be executing

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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜