seany123 Posted September 11, 2011 Share Posted September 11, 2011 I have a star rating system which uses this update php and js: PHP if($_GET['do']=='rate'){ // do rate rate($show, $_GET['id']); } else if($_GET['do']=='getrate'){ // get rating getRating($show, $_GET['id']); } // function to retrieve function getRating($show, $url){ $rs=@mysql_fetch_array($result); // set width of star $rating = (@round($rs[value] / $rs[counter],1)) * 20; echo $rating; } // function to insert rating function rate($show, $url){ global $dbh; $text = strip_tags($_GET['']); $update = "update `" . $show . "_episodes` set counter = counter + 1, value = value + ".$_GET['rating']." where md5(url)='".$url."'"; $result = mysql_query($update); JS // JavaScript Document $(document).ready(function() { // get rating function function getRating(show, id){ $.ajax({ type: "GET", url: "../update.php", data: "do=getrate&id="+id+"&show="+show, cache: false, async: false, success: function(result) { // apply star rating to element $("#current-rating-"+id+"").css({ width: "" + result + "%" }); }, error: function(result) { alert("some error occured, please try again later"); } }); } // link handler $('.ratelinks li a').click(function(){ // get the parent id var idStar = $(this).parent().parent().attr('id'); var show = $(this).parent().parent().attr('show'); $.ajax({ type: "GET", url: "../update.php", data: "rating="+$(this).text()+"&do=rate&show="+show+"&id="+idStar, cache: false, async: false, success: function(result) { // remove #ratelinks element to prevent another rate $("#ratelinks").remove(); // get rating after click getRating(show, idStar); }, error: function(result) { alert("some error occured, please try again later"); } }); }); }); Now i decided i wanted to show the percentage so i added this to the PHP: // function to retrieve Percentage function getPercentage($show, $url){ $per=@mysql_fetch_array($result); // set percentage if ($per[counter] >= 1){ $percentage = (round($per[value] * 100) / ($per[counter]*5)); echo "( <FONT COLOR=RED>".$percentage."%</FONT> from ".$per[counter]." votes )"; } else if ($per[counter] < 1){ echo "(No Votes Yet!)"; } } Which does work, but requires a page reload to make it show the new percentage... (whereas the stars update automatically) so how can i set $per[counter], $per[value] and $percentage? using the javascript? Seany Link to comment https://forums.phpfreaks.com/topic/246876-how-to-set-new-values-to-a-variable/ Share on other sites More sharing options...
trq Posted September 11, 2011 Share Posted September 11, 2011 Instead of simply returning a html fragment in your php code, use json_encode to return multiple values as a json object. Link to comment https://forums.phpfreaks.com/topic/246876-how-to-set-new-values-to-a-variable/#findComment-1267891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.