Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Location
    Knoxville, TN
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help checking if radio buttons are selected

    Hi everyone,

    The code I wrote 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 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 Code:
    <?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 == ) { 
                
    $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 == ) { 
                
    $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 == ) { 
                
    $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.

    I've been told the checking is done through javascript but I am clueless when it comes to javascript.

    Thanks for any help you can give me.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Javascript is not suitable for this sort of thng as the user can simply disable it. Your checking should be carried out server-side.



    As I prepare this script, tapping away at the keyboard as Socrates might have done had he owned a PC ....... - Football writer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •