ajax to get the productid when the page loads?

Aug 17, 2014 at 10:53 PM
Edited Aug 17, 2014 at 10: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.

HTML
<!DOCTYPE html>
<html lang="en">
<?php require 'core/init.php'; ?>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>    
        <meta charset="utf-8">
    </head>
    <body>
        <div>
            <table>
                <tr>
                    <td><div id="randomtest" class="star"></div></td>
                    <td id="response"></td>
                </tr>
            </table> 
        </div>
        
        <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';
            $('.star').raty({
                
                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');
                
                    $.ajax({
                    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>');
                    }
                    });
                }
            });
        });
        </script>
    </body>
</html>
PHP
<?php
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")){
    if($voteCh->num_rows){
        while($row = $voteCh->fetch_object()){
            $voteCo[] = $row;
        }
        $voteCh->free();
    }
}

$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){
    if(!count($voteCo)){
        echo 'no voteCo';
    }else{
        foreach ($voteCo as $r){
            echo escape($r->votecount);
        }
        die();
    }
    }else{
        echo "error updating database";
}


?>
Aug 18, 2014 at 2:45 PM
Edited Aug 18, 2014 at 2: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);
        });