ajax to get the productid when the page loads?

Aug 17, 2014 at 11:53 PM
Edited Aug 17, 2014 at 11:57 PM
Hello all

I know its probably a faux pas but i have used both rateit and raty to create my 'working' star rating system but im having issues getting the current votes to work on page load and go in the 'response' element (im going to rename it) I'll add the 2 pieces of code that make t function to give an idea of where i am and will make a 2nd php file for this if needed.

If anyone could offer a suggestion id much appreciate it.

Thanks in advance.

<!DOCTYPE html>
<html lang="en">
<?php require 'core/init.php'; ?>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>    
        <meta charset="utf-8">
                    <td><div id="randomtest" class="star"></div></td>
                    <td id="response"></td>
        <script type="text/javascript" src="star/assets/js/jquery.raty.min.js"></script>
        <script type="text/javascript">
        $(function() {
            $.fn.raty.defaults.path = 'star/assets/img';
                half      : true,
                number    : 5,
                score     : 3,
                click: function(score, evt) {
                /*implement this for only click once 
                 * $(this).find('img').unbind('click');
                var pid=$(this).prop('id');
                    url: 'rate.php',
                    data: { score: score, pid:pid },
                    type: 'POST',
                    success: function (data) {
                        $('#response').empty().append(data + ' Votes.'); // this needs to be done on page load too!
                    error: function (jxhr, msg, err) {
                        $('#response').append('<li style="color:red">' + msg + '</li>');
require 'core/init.php';

$_SESSION['score'] = $_POST['score'];
$_SESSION['pid'] = $_POST['pid'];

$score  = $_SESSION['score'];
$pid    = $_SESSION['pid'];
$ipaddy = $_SERVER['REMOTE_ADDR'];
$voteCo =array();

if($voteCh = $db->query("SELECT * FROM votes")){
        while($row = $voteCh->fetch_object()){
            $voteCo[] = $row;

$udVote = $db->query("INSERT INTO ratings (score, pid, ip) VALUES ('{$score}','{$pid}','{$ipaddy}')") && $db->query("UPDATE votes SET votecount = votecount +1 WHERE pid='$pid'");

if ($udVote){
        echo 'no voteCo';
        foreach ($voteCo as $r){
            echo escape($r->votecount);
        echo "error updating database";

Aug 18, 2014 at 3:45 PM
Edited Aug 18, 2014 at 3:45 PM
its ok thanks i sorted it cheers just needed this after a lil bit of a rewrite.
//update votes count on page load
        $('.star').each(function () {
            var pid=$(this).prop('id');
                $.post('votes.php', { pid:pid } ,onRateResultsSuccess).fail(onRateResultsError);