...

View Full Version : need help checking if radio buttons are selected



Mike Smith
05-28-2009, 01:48 AM
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
// 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.

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.

Philip M
05-28-2009, 08:24 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum