Jump to content

[SOLVED] Problem with script submitting/radio buttons


fudmonkey

Recommended Posts

Let me just begin by saying that (especially here) I'm the php equivalent of a retard. I usually just play around with stuff like this until I figure it own on my own, but I've spent way too much time on this one, so I figured I'd pose the question to people who can probably solve it in less time than it took me to register on this forum.

 

I'm trying to implement a very simple php-based rating system on my site. The script I'm using contains 5 radio buttons and a 'submit' link, but for both ease of use and aesthetic purposes, I'm trying to get rid of the submit button, and turn the radio buttons themselves into image links that will just directly call each specific rating value when clicked. I've been trying to use onclick="this.form.submit()", but it's not working at all.

 

Here's my first try:

http://www.fudmonkey.com/rater/rater.php

 

Second version, similar script:

http://www.fudmonkey.com/phprater/rate.php

 

Please let me know what I'm doing wrong. Thanks!

Link to comment
Share on other sites

I've been trying to use onclick="this.form.submit()", but it's not working at all.

 

Works for me. Did you check if any errors are being thrown? By the way, use some line breaks in your HTML code. It's a pain to try and rework the HTML into a readable format when trying to help someone.

Link to comment
Share on other sites

Sorry guys, I thought you'd be able to just view source on the pages themselves, but I suppose that doesn't work the same way...?

 

Here are both versions. Maybe one will work better than the other...

 

<?

// User settings
$rater_ip_voting_restriction = true; // restrict ip address voting (true or false)
$rater_ip_vote_qty=1; // how many times an ip address can vote
$rater_already_rated_msg="YOU'VE ALREADY RATED THIS EPISODE";
$rater_not_selected_msg="PLEASE SELECT A RATING";
$rater_thankyou_msg="THANKS FOR THE OPINION!";
$rater_generic_text="THIS EPISODE"; // generic item text
$rater_end_of_line_char="\n"; // may want to change for different operating systems



if(!isset($rater_id)) $rater_id=1;
if(!isset($rater_item_name)) $rater_item_name=$rater_generic_text;


// DO NOT MODIFY BELOW THIS LINE
$rater_filename='item_'.$rater_id.".rating";
$rater_rating=0;
$rater_stars="";
$rater_stars_txt="";
$rater_rating=0;
$rater_votes=0;
$rater_msg="";

// Rating action
if(isset($_REQUEST["rate".$rater_id])){
if(isset($_REQUEST["rating_".$rater_id])){
  while(list($key,$val)=each($_REQUEST["rating_".$rater_id])){
   $rater_rating=$val;
  }
  $rater_ip = getenv("REMOTE_ADDR");
  $rater_file=fopen($rater_filename,"a+");
  $rater_str="";
  $rater_str = rtrim(fread($rater_file, 1024*,$rater_end_of_line_char);
  if($rater_str!=""){
   if($rater_ip_voting_restriction){
    $rater_data=explode($rater_end_of_line_char,$rater_str);
$rater_ip_vote_count=0;
    foreach($rater_data as $d){
 $rater_tmp=explode("|",$d);
 $rater_oldip=str_replace($rater_end_of_line_char,"",$rater_tmp[1]);
 if($rater_ip==$rater_oldip){
  $rater_ip_vote_count++;
 }
    }
if($rater_ip_vote_count > ($rater_ip_vote_qty - 1)){
     $rater_msg=$rater_already_rated_msg;
}else{
     fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
     $rater_msg=$rater_thankyou_msg;
}
   }else{
    fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
    $rater_msg=$rater_thankyou_msg;
   }
  }else{
   fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
   $rater_msg=$rater_thankyou_msg;
  }
  fclose($rater_file);
}else{
  $rater_msg=$rater_not_selected_msg;
}
}

// Get current rating
if(is_file($rater_filename)){
$rater_file=fopen($rater_filename,"r");
$rater_str="";
$rater_str = fread($rater_file, 1024*;
if($rater_str!=""){
  $rater_data=explode($rater_end_of_line_char,$rater_str);
  $rater_votes=count($rater_data)-1;
  $rater_sum=0;
  foreach($rater_data as $d){
   $d=explode("|",$d);
   $rater_sum+=$d[0];
  }
  $rater_rating=number_format(($rater_sum/$rater_votes), 2, '.', '');
}
fclose($rater_file);
}else{
$rater_file=fopen($rater_filename,"w");
fclose($rater_file);
}

// Assign star image
if ($rater_rating <= 0  ){$rater_stars = "./img/00star.gif";$rater_stars_txt="";}
if ($rater_rating >= 0.5){$rater_stars = "./img/05star.gif";$rater_stars_txt="";}
if ($rater_rating >= 1  ){$rater_stars = "./img/1star.gif";$rater_stars_txt="";}
if ($rater_rating >= 1.5){$rater_stars = "./img/15star.gif";$rater_stars_txt="";}
if ($rater_rating >= 2  ){$rater_stars = "./img/2star.gif";$rater_stars_txt="";}
if ($rater_rating >= 2.5){$rater_stars = "./img/25star.gif";$rater_stars_txt="";}
if ($rater_rating >= 3  ){$rater_stars = "./img/3star.gif";$rater_stars_txt="";}
if ($rater_rating >= 3.5){$rater_stars = "./img/35star.gif";$rater_stars_txt="";}
if ($rater_rating >= 4  ){$rater_stars = "./img/4star.gif";$rater_stars_txt="";}
if ($rater_rating >= 4.5){$rater_stars = "./img/45star.gif";$rater_stars_txt="";}
if ($rater_rating >= 5  ){$rater_stars = "./img/5star.gif";$rater_stars_txt="";}


// Output
echo '<div align="center" class="hreview">';
echo '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
echo '<h4 class="item" style="font-weight: bold;font-family: Verdana;color:#ffcc00;">RATE <span class="fn">'.$rater_item_name.'</span></h4>';
echo '<span  class="rating"><img src="'.$rater_stars.'?x='.uniqid((double)microtime()*1000000,1).'" alt="'.$rater_stars_txt.' stars" /> <br><h5 style="font-weight: bold;font-family: Verdana;"><span class="reviewcount"> '.$rater_votes.' VOTES</span></h5>';
echo '<label for="rate5_'.$rater_id.'"><input type=image src="img/5button.gif" value="5" name="rating_'.$rater_id.'[]" id="rate5_'.$rater_id.'" onclick="this.form.submit();"/></label>';
echo '<br><label for="rate4_'.$rater_id.'"><input type=image src="img/4button.gif" value="4" name="rating_'.$rater_id.'[]" id="rate4_'.$rater_id.'" /></label>';
echo '<br><label for="rate3_'.$rater_id.'"><input type=image src="img/3button.gif" value="3" name="rating_'.$rater_id.'[]" id="rate3_'.$rater_id.'" /></label>';
echo '<br><label for="rate2_'.$rater_id.'"><input type=image src="img/2button.gif" value="2" name="rating_'.$rater_id.'[]" id="rate2_'.$rater_id.'" /></label>';
echo '<br><label for="rate1_'.$rater_id.'"><input type=image src="img/1button.gif" value="1" name="rating_'.$rater_id.'[]" id="rate1_'.$rater_id.'" /></label>';
echo '<br><input type="hidden" name="rs_id" value="'.$rater_id.'" />';
echo '<input type="submit" name="rate'.$rater_id.'" value="RATE!" />';
if($rater_msg!="") echo "<div>".$rater_msg."</div>";
echo '</form>';
echo '</div>';


?>

 

<?
extract($HTTP_GET_VARS);
extract($HTTP_POST_VARS);
/********* PHP RATING SYSTEM v1.5 ************
Copyright Scriptsez.net
You have to leave the copyright.
If you have any problem just let us know.
E-mail: support@scriptsez.net
Website: http://www.scriptsez.net
**********************************************/
$ficdest=explode(".",basename($PHP_SELF));
$ficdest=$ficdest[0].".dat";
$ip = getenv(REMOTE_ADDR);
if(file_exists($ficdest)) {
     $compteur=fopen($ficdest, "r");
     $old_stats=file($ficdest);
     $stats=explode("|", $old_stats[0]);
     fclose($compteur);
     $new_count=$stats[0];
     if ($stats[3] != $ip) {
         $new_count +=1;
     }
     $ip_hit=$ip;
     $compteur=fopen($ficdest, "w");
     fputs($compteur, "$new_count|$stats[1]|$stats[2]|$ip_hit|$stats[4]");
     fclose($compteur);
}
else {
     $nouveau_compteur=fopen($ficdest, "w");
     fputs($nouveau_compteur, "1|||$ip|");
     fclose($nouveau_compteur);
}

if (!empty($envoi)) {
     $vote=fopen($ficdest, "r");
     $old_stats=file($ficdest);
     $stats=explode("|", $old_stats[0]);
     fclose($vote);
     $nbr_votes=$stats[1];
     $moy_votes=$stats[2];
     if ($stats[4] != $ip) {
         $nbr_votes +=1;
         $moy_votes=((($stats[1]*$stats[2])+$note)/$nbr_votes);
     }
else { echo "<font face=Verdana size=2 color=red>You have already voted</font>"; }
     $ip_vote=$ip;
     $vote=fopen($ficdest, "w");
     $new_stats=fputs($vote, "$new_count|$nbr_votes|$moy_votes|$stats[3]|$ip_vote");
     fclose($vote);
}

print ("<form method=post>");
$old_stats=file($ficdest);
$stats=explode("|", $old_stats[0]);
if ($stats[2] == 5)
{
$star = "images/5star.gif" ;
}
if ($stats[2]>=1)
{
$star = "images/1star.gif" ;
}
if ($stats[2]>=1.5)
{
$star = "images/15star.gif" ;
}
if ($stats[2]>=2)
{
$star = "images/2star.gif" ;
}
if ($stats[2]>=2.5)
{
$star = "images/25star.gif" ;
}
if ($stats[2]>=3)
{
$star = "images/3star.gif" ;
}
if ($stats[2]>=3.5)
{
$star = "images/35star.gif" ;
}
if ($stats[2] >= 4)
{
$star = "images/4star.gif" ;
}
if ($stats[2] >= 4.5)
{
$star = "images/45star.gif" ;
}
if ($stats[2] >= 5)
{
$star = "images/5star.gif" ;
}
if ($stats[2]<=0)
{
$star = "images/00star.gif" ;
}
print ("<table bordercolor=#99999 cellspacing=0 border=0><td width=50% cellspacing=none cellpadding=none align=center valign=middle border=1><font size=1 face=Verdana color=#999999><img src=\"$star\" alt=\"Average rating: $stats[2]\">  <br>$stats[1] RATINGS</td><tr></font>");
echo"<td align=center valign=middle><input type=radio name=note value=5><font face=arial size=2 color=#99999>JAWSOME!";
echo"<br><input type=radio name=note value=4 onclick="this.form.submit()">GREAT";
echo"<br><input type=radio name=note value=3>GOOD";
echo"<br><input type=radio name=note value=2>BAD";
echo"<br><input type=radio name=note value=1>PTERODACTYL SHIT";
print ("<input type=hidden name=envoi value=1>  <br><input type=submit value=RATE! style=background:#ffcc00;border-width:1;Border-color:#ffcc00;></table></form></font></td>");

?>

Link to comment
Share on other sites

You can't put double quotes within a strng delimited with double quotes unless you escape them. This is the problem line:

echo"<br><input type=radio name=note value=4 onclick="this.form.submit()">GREAT";

 

When the line is parsed it thinks that the double quote at the beginning of the onclick is ending the string you want to echo.

 

Change that line to thie

echo"<br><input type=radio name=note value=4 onclick=\"this.form.submit()\">GREAT";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.