Jump to content

Three voting blocks - STOPPING the same answer being submitted more than once


Mike Smith

Recommended Posts

Hi everyone, I recently had a php problem for a script I'm writing and had some great help from MadTechie here and am back with another brain stumper. I've dug into the codes, added/edited/removed and have my codes set where I need them.

 

However, the code is showing three random results from the database and each result gets three options to choose from (spoon, fork, knife). What I need to figure out is a way to make sure each image thats showing up gets one vote - and the person cannot vote on the same answer for pic2 and pic3 (ie: if they vote "spoon" for pic1, I need it to give a "please vote again" message if they click the same answer for pic2 or pic3).

 

Here's my index.php code

 

<?php 
// Connects to your Database 
$link = mysql_connect('localhost', 'username', 'password'); 
mysql_select_db("database") or die(mysql_error()); 

?>

<?php include('header.php'); ?>

<div class="contentwide">
<div class="contentwrap">
<form method="post" action="voted.php"> 
<?php 
$query = "SELECT id, name, url FROM photos WHERE category!= 'Xrated' ORDER BY RAND() LIMIT 3";
$result = mysql_query($query); 
while($row = mysql_fetch_assoc($result)) 
{ 
?> 
    <input type="hidden" name="id[]" value="<?php echo $row['id']; ?>">
    <input type="hidden" name="page" value="index.php"> 
    <div class="picturewrap"> 
        <img src="<?php echo $row['url']; ?>" alt="<?php echo $row['name']; ?>" /> 
        <div class="formwrapper"> 
	     <div class="formspoon">
	     <img src="images/form_spoon.jpg" alt="" /><br />
	     <input type="radio" name="images[<?php echo $row['id']; ?>]" value="1"> 
	     </div>
        </div> 
        <div class="formwrapper"> 
	     <div class="formspoon">
	     <img src="images/form_knife.jpg" alt="" /><br />
	     <input type="radio" name="images[<?php echo $row['id']; ?>]" value="3"> 
	     </div>
        </div> 
        <div class="formwrapper"> 
                 <div class="formspoon">
	     <img src="images/form_fork.jpg" alt="" /><br />
	     <input type="radio" name="images[<?php echo $row['id']; ?>]" value="2"> 
	     </div>
        </div> 
    </div> 
<?php 
} 
?> 
    <div class="submitform"> 
        <input type="image" src="images/button_submit.jpg" class="button_submit" value="Submit"><br/ > 
    </div> 
</form> 
</div>
</div>
<?php mysql_close($link); ?>
<?php include('footer.php'); ?>

 

Here is my voted.php file

 

<?php 

// Connects to your Database 
$link = mysql_connect('localhost', 'user', 'password'); 
mysql_select_db("database") or die(mysql_error()); 

    $page = $_POST['page']; 

if ( !empty( $_POST['images'] ) ) { 
    $images = $_POST['images']; 
    // Loop over each item in the $images array. 
    foreach ( $images as $id => $vote ) { 
        if ( $vote == 1 ) { 
            $update = sprintf( "UPDATE photos SET spoon=spoon+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" );
	header("Location: $page");  
        } elseif ( $vote == 2 ) { 
            $update = sprintf( "UPDATE photos SET fork=fork+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" ); 
	header("Location: $page"); 
        } elseif ( $vote == 3 ) { 
            $update = sprintf( "UPDATE photos SET knife=knife+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" ); 
	header("Location: $page"); 
        } else 
            die( "Invalid value passed as a vote." ); 
    } 
} 

?>

 

Any ideas? (the site is being built similar to www.fmarrykill.net if you'd like to see a live working example of how the "don't vote twice" thing goes.

 

Thanks for any help you can give me. I'm starting to get the hang of php and I have to say, I really enjoy it and also am grateful that this forum is here to help guide me :)

 

this is a example of the JavaScript your need to match the forms that are clicked via radio boxs.

 

var rg = (document.forms.FORMNAME.match.len­ gth == undefined

                ? new Array(document.forms.FORMNAME.matc­ h)

                : document.forms.FORMNAME.match);­

  var c = 0;

  for (var i=0; i<rg.length; rg++) {

    if (rg.checked) c++;

  }

  if (!c) { alert('Please select one of the items.'); }

redarrow, so the FORMNAME would be what I have in the <form name=""> spot? I can kind of understand the codes and the !c aspects of it, but am lost as to what it would do/what I need to do. I apologize if I sound like an idiot :)

it not easy, so far got this but, it way off, it very hard, i am trying betend the 1234 are pictures and the rest are cheek boxes, but i am finding it hard.

 

<html>
      <head>
      <script type="text/javascript">
      window.onload=function()
      {
          
          document.getElementById("ok_button").onclick = checkallradio;
          document.getElementById("mindre_button").onclick = checkallradio;
          document.getElementById("store_button").onclick = checkallradio;
          document.getElementById("notdone_button").onclick = checkallradio;
      };
      
      function checkallradio() {
         for (var j=0;j < document.test.length;j++)
         {
              if (document.test.elements[j].type == 'radio') {
                  objEl = document.test.elements[j];
                  //Test which 'checkall' button was clicked. 'this'
                  //refers to the button that was clicked:
                  switch (this.id) {
                      case "ok_button":
                          //then check first radio button
                          //in the group:
                          document.test.elements[objEl.name][0].checked=true;
                          break;
                      case "mindre_button":
                          //then check second radio button
                          //in the group:
                          document.test.elements[objEl.name][1].checked=true;
                          break;
                      case "store_button":
                          //then check third radio button
                          //in the group:
                          document.test.elements[objEl.name][2].checked=true;
                          break;
                      case "notdone_button":
                                //then check fourth radio button
                                //in the group:
                                document.test.elements[objEl.name][3].checked=true;
                                break;
                  }
                
              }
          }
      }
      </script>
      </head>
      <body>
      <form name="test">
      <input type="button" id="ok_button" value="1">
      <input type="button" id="mindre_button" value="2">
      <input type="button" id="store_button" value="3">
      <input type="button" id="notdone_button" value="4"><br>
      <input type="radio" value="1" name="radio1"><input type="radio" value="2" name="radio1"><input type="radio" value="3" name="radio1"><input type="radio" value="4" name="radio1"><br>
      <input type="radio" value="1" name="radio2"><input type="radio" value="2" name="radio2"><input type="radio" value="3" name="radio2"><input type="radio" value="4" name="radio2"><br>
      <input type="radio" value="1" name="radio3"><input type="radio" value="2" name="radio3"><input type="radio" value="3" name="radio3"><input type="radio" value="4" name="radio3"><br>
      
      
      
      </form>
</body>
</html>

I've been trying to mess with the code you last posted but can't figure out how to get over the hump with the code. Theres three choices and theres four in your code. Threw me off lol. I do appreciate your help, along with anyone else who can chime in :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.