jacko_162 Posted August 11, 2011 Share Posted August 11, 2011 I have 2 pages which is for a rating system with stars. index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Ajax Rating System: Create Simple Ajax Rating System using jQuery AJAX and PHP : 99Points.Info</title> <script type="text/javascript" src="jquery-1.2.6.min.js"></script> <link href="99points.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> // <![CDATA[ $(document).ready(function(){ $('#loader').hide(); $('#inner').children().click(function(){ var a = $(this).attr("id"); $.post("rating.php?value="+a, { }, function(response){ $('#inner').fadeOut(); $('#inner').html(unescape(response)); $('#inner').fadeIn(); setTimeout("hideMesg();", 2000); }); }); }); function hideMesg(){ $('.rating_message').fadeOut(); $.post("rating.php?show=1", { }, function(response){ $('#inner').html(unescape(response)); $('#inner').fadeIn('slow'); }); } // ]]> </script> </head> <body> <?php include("dbcon.php"); $result=mysql_query("select sum(rating) as rating from ratings"); $row=mysql_fetch_array($result); $rating=$row['rating']; $product_id = $_get[iD]; $quer = mysql_query("select rating from ratings"); $all_result = mysql_fetch_assoc($quer); $rows_num = mysql_num_rows($quer); if($rows_num > 0){ $get_rating = floor($rating/$rows_num); $rem = 5 - $get_rating; } else { $rem = 5; }?> <h1>Ajax Test Rate System.</h1> <div align="center" style="margin:30px;"> <div id="container"> <img src="2.gif" alt="" height="300" /> <div id="outer"> <div id="inner"> <?php for($k=1;$k<=$get_rating;$k++){?> <div class="rating_enb" id="<?php echo $k?>"> </div> <?php }?> <?php for($i=$rem;$i>=1;$i--){?> <div class="rating_dis" id="<?php echo $k?>"> </div> <?php $k++; }?> <div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?>.0 (<?php echo $rows_num?>)</div> </div> </div> </div> </div> </body> </html> then the rating.php (does the functions) <?php include("dbcon.php"); //echo $_SERVER['REMOTE_ADDR'].'--' . $_REQUEST['value']; echo $_REQUEST['id']; $users_ip = $_SERVER['REMOTE_ADDR']; if($_REQUEST['value']) { $id = $_REQUEST['value']; $result = mysql_query("select users_ip from ratings where users_ip='$users_ip'"); $num = mysql_num_rows($result); if($num==0) { $query = "insert into ratings (rating,users_ip) values ('$id','$users_ip')"; mysql_query( $query); $result=mysql_query("select sum(rating) as rating from ratings"); $row=mysql_fetch_array($result); $rating=$row['rating']; $quer = mysql_query("select rating from ratings"); $all_result = mysql_fetch_assoc($quer); $rows_num = mysql_num_rows($quer); if($rows_num > 0){ $get_rating = floor($rating/$rows_num); $rem = 5 - $get_rating; } ?> <?php echo $_get['id']; ?> <?php for($k=1;$k<=$get_rating;$k++){?> <div class="rating_enb" id="<?php echo $k?>"> </div> <?php }?> <?php for($i=$rem;$i>=1;$i--){?> <div class="rating_dis" id="<?php echo $k?>"> </div> <?php $k++; }?> <div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?>.0 (<?php echo $rows_num?>)</div> <?php } else { echo '<div class="rating_message">You have already Voted for this product!!</div>'; } } if(@$_REQUEST['show']) // show rating again after showing message { $result=mysql_query("select sum(rating) as rating from ratings"); $row=mysql_fetch_array($result); $rating=$row['rating']; $quer = mysql_query("select rating from ratings"); $all_result = mysql_fetch_assoc($quer); $rows_num = mysql_num_rows($quer); if($rows_num > 0){ $get_rating = floor($rating/$rows_num); $rem = 5 - $get_rating; }?> <?php for($k=1;$k<=$get_rating;$k++){?> <div class="rating_enb" id="<?php echo $k?>"> </div> <?php }?> <?php for($i=$rem;$i>=1;$i--){?> <div class="rating_dis" id="<?php echo $k?>"> </div> <?php $k++; }?> <div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?>.0 (<?php echo $rows_num?>)</div> <?php }?> the script finds my ip address fine, and the value is forwarded and both added to my database. what i wanted to add onto this is an id number in the URL. namely "rating.php?ID=47" this is then going to be inserted into the db in the rating.php file. can someone help me code this please? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 11, 2011 Share Posted August 11, 2011 you appear to have everything you need already in your code? are you getting an error of some kind? or the result isn't what you expect it to be? What exactly is the issue you're having? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 if you're adding &id=xx, then echo $_REQUEST['id']; will work. if you're adding &ID=xx then you need to use uppercase: echo $_REQUEST['ID']; Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted August 11, 2011 Share Posted August 11, 2011 you appear to have everything you need already in your code? are you getting an error of some kind? or the result isn't what you expect it to be? What exactly is the issue you're having? Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 11, 2011 Author Share Posted August 11, 2011 The Script works fine, i wanted to make the stars script work on multiple products, so i added "product_id" column to my database. i can pass a url such as index.php?ID=XX but i am not sure how to alter the above code to pull the id from URL, pass it through the script and eventually add the product_id to the database. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 (..) but i am not sure how to alter the above code to pull the id from URL (..) Just gave you that solution. Use $_REQUEST or $_GET Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 11, 2011 Author Share Posted August 11, 2011 i tried: $pid = $_GET['ID']; in index.php, but i couldnt echo this or could i pass this onto rating.php Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 can also post the following: 1. how you called the url. (the complete string please) 2. what's the name of the file where you have that code 3. the part where you actually echo that variable. thank you Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 11, 2011 Author Share Posted August 11, 2011 if you see my first post i have the 2 pages needed "index.php" and "ratings.php" i then typed index.php?ID=47 and in index .php i have the following: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Ajax Rating System: Create Simple Ajax Rating System using jQuery AJAX and PHP : 99Points.Info</title> <script type="text/javascript" src="jquery-1.2.6.min.js"></script> <link href="99points.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> // <![CDATA[ $(document).ready(function(){ $('#loader').hide(); $('#inner').children().click(function(){ var a = $(this).attr("id"); $.post("rating.php?value="+a, { }, function(response){ $('#inner').fadeOut(); $('#inner').html(unescape(response)); $('#inner').fadeIn(); setTimeout("hideMesg();", 2000); }); }); }); function hideMesg(){ $('.rating_message').fadeOut(); $.post("rating.php?show=1", { }, function(response){ $('#inner').html(unescape(response)); $('#inner').fadeIn('slow'); }); } // ]]> </script> </head> <body> <?php include("dbcon.php"); $result=mysql_query("select sum(rating) as rating from ratings"); $row=mysql_fetch_array($result); $rating=$row['rating']; $pid = $_get['ID']; $quer = mysql_query("select rating from ratings"); $all_result = mysql_fetch_assoc($quer); $rows_num = mysql_num_rows($quer); if($rows_num > 0){ $get_rating = floor($rating/$rows_num); $rem = 5 - $get_rating; } else { $rem = 5; }?> <h1>Ajax Test Rate System.</h1> <div align="center" style="margin:30px;"> <div id="container"> <img src="2.gif" alt="" height="300" /> <div id="outer"> <div id="inner"> <?php for($k=1;$k<=$get_rating;$k++){?> <div class="rating_enb" id="<?php echo $k?>"> </div> <?php }?> <?php for($i=$rem;$i>=1;$i--){?> <div class="rating_dis" id="<?php echo $k?>"> </div> <?php $k++; }?> <div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?>.0 (<?php echo $rows_num?>)</div> </div> </div> </div> </div> </body> </html> im actually not sure where and how it calls rating.php i assume its in the javascript, of which i have NO clue.. i need to pass the ?ID=47 to rating.php and from there i assume i need to use the $_GET and insert the result into the "product_id" column in Database. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 you need to pay more attention. you said you tried: $pid = $_GET['ID']; but that's NOT what you have. (see why I wanted you to post your code again?) this: $pid = $_get['ID']; should be: $pid = $_GET['ID']; (UPPERCASE) Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 11, 2011 Author Share Posted August 11, 2011 haha always the easy things, now i have the $pid variable sorted in index.php, how do i edit index.php to pass this onto rating.php? then i can add it into the INSERT. Thanks WebStyles o/ Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted August 11, 2011 Share Posted August 11, 2011 concatenate it onto a querystring to rating.php.. Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 11, 2011 Author Share Posted August 11, 2011 managed to blag it by adding a second variable in the javascript and adding it to the URL string... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.