Jump to content

[SOLVED] Multiple Radio Buttons on Same Page - single submit button


superfly

Recommended Posts

Hi All,

 

I've been searching this forum for some help with a particular issue i'm having.

The application i'm working on is a photo challenge - users submit photos and have them ranked by other users.

 

I have decided to use a rating system from 1 - 10 (10 radio buttons to be exact).

For a particular challenge - all entries are displayed with the voting scale underneath each image.

The user will go through each image, and click the rating they wish to give that image and then scroll down to the next image, etc...

at the bottom of the page, there is a single submit button to submit the whole page.

 

My problem is i am having trouble visualising how to process the various radio button values into the database.

 

+--------------+------------+------+-----+---------+----------------+

| Field        | Type      | Null | Key | Default | Extra          |

+--------------+------------+------+-----+---------+----------------+

| vote_id      | int(11)    | NO  | PRI | NULL    | auto_increment |

| photo_id    | int(11)    | NO  |    | NULL    |                |

| user_id      | int(11)    | NO  |    | NULL    |                |

| chlng_id    | int(11)    | NO  |    | NULL    |                |

| voter_rating | tinyint(2) | NO  |    | NULL    |                |

+--------------+------------+------+-----+---------+----------------+

 

above is the votes table structure - the value of the selected radio button should be entered in the voter_rating column.

 

Below is sample code for the voting radio buttons, the name is generated automatically based on the photo_id, this makes the radio button groupings unique and i figured that since only one value is returned per group, i could differentiate them by the photo_id.

 

<table border="0" align="center">

          <tr>

 

            <td><input type="radio" name="1007" id="vote1" value="1" /></td>

            <td><input type="radio" name="1007" id="vote2" value="2" /></td>

            <td><input type="radio" name="1007" id="vote3" value="3" /></td>

            <td><input type="radio" name="1007" id="vote4" value="4" /></td>

            <td><input type="radio" name="1007" id="vote5" value="5" /></td>

            <td><input type="radio" name="1007" id="vote6" value="6" /></td>

            <td><input type="radio" name="1007" id="vote7" value="7" /></td>

            <td><input type="radio" name="1007" id="vote8" value="8" /></td>

            <td><input type="radio" name="1007" id="vote9" value="9" /></td>

 

            <td><input type="radio" name="photo_id.1007" id="vote10" value="10" /></td>

          </tr>

          <tr>

            <td>1</td>

            <td>2</td>

            <td>3</td>

          <td>4</td>

 

            <td>5</td>

            <td>6</td>

            <td>7</td>

            <td>8</td>

            <td>9</td>

            <td>10</td>

 

          </tr>

</table>

 

The ultimate goal is to have a loop that goes through the votes and carries out the following insert for each vote:

 

$sql = "INSERT INTO upc_votes SET photo_id = '$photo_id', user_id = '$user_id', chlng_id = '$chlng_id',description = '$desc', voter_rating = '$voter_rating'";

 

Thanks!

 

Link to comment
Share on other sites

user_id will be the id of the user that voted, in order to avoid duplicate votes, it is not currently being used and is just set to 0 (zero)

chlng_id is the unique id of the challenge (it corresponds to another table for the challenges).

 

[edit]Sorry, i just realised the sql is incorrect

 

it should be: $sql = "INSERT INTO upc_votes SET photo_id = '$photo_id', user_id = '$user_id', chlng_id = '$chlng_id', voter_rating = '$voter_rating'";

 

 

 

 

Link to comment
Share on other sites

ok this is fairly nice to...

you are going to use html arrays for your radio buttons and they are all going to use the id of teh image as there index....

so below will be the markup for the vote buttons for images with id of 12, 67 and 82...

<tr>
<td><input type="radio" name="vote[12]" value="1" /></td>
<td><input type="radio" name="vote[12]" value="2" /></td>
<td><input type="radio" name="vote[12]" value="3" /></td>
<td><input type="radio" name="vote[12]" value="4" /></td>
<td><input type="radio" name="vote[12]" value="5" /></td>
<td><input type="radio" name="vote[12]" value="6" /></td>
<td><input type="radio" name="vote[12]" value="7" /></td>
<td><input type="radio" name="vote[12]" value="8" /></td>
<td><input type="radio" name="vote[12]" value="9" /></td>
<td><input type="radio" name="vote[12]" value="10" /></td>
</tr>
<tr>
<td><input type="radio" name="vote[67]" value="1" /></td>
<td><input type="radio" name="vote[67]" value="2" /></td>
<td><input type="radio" name="vote[67]" value="3" /></td>
<td><input type="radio" name="vote[67]" value="4" /></td>
<td><input type="radio" name="vote[67]" value="5" /></td>
<td><input type="radio" name="vote[67]" value="6" /></td>
<td><input type="radio" name="vote[67]" value="7" /></td>
<td><input type="radio" name="vote[67]" value="8" /></td>
<td><input type="radio" name="vote[67]" value="9" /></td>
<td><input type="radio" name="vote[67]" value="10" /></td>
</tr>
<tr>
<td><input type="radio" name="vote[82]" value="1" /></td>
<td><input type="radio" name="vote[82]" value="2" /></td>
<td><input type="radio" name="vote[82]" value="3" /></td>
<td><input type="radio" name="vote[82]" value="4" /></td>
<td><input type="radio" name="vote[82]" value="5" /></td>
<td><input type="radio" name="vote[82]" value="6" /></td>
<td><input type="radio" name="vote[82]" value="7" /></td>
<td><input type="radio" name="vote[82]" value="8" /></td>
<td><input type="radio" name="vote[82]" value="9" /></td>
<td><input type="radio" name="vote[82]" value="10" /></td>
</tr>

now the lovely bit...

you can now use the indices of your array to contruct just one query on the database.

<?php
$qry = "INSERT INTO `upc_votes` (`photo_id`, `user_id`, `chlng_id`, `voter_rating`) VALUES ";

foreach ($_POST['vote'] as $key => $val)
{
$qry .= "('$key', '$user_id', '$chlng_id',  '$val') ,";
}

$qry = substr($qry, 0 , -1);

$res = mysql_query($qry);
?>

Link to comment
Share on other sites

what a champion!!

 

I swear, as i made a breakthrough, i received the email of your post - only my solution was slightly different:

I had some idea that there would be an array in there somewhere...

 

below is my html echo statement.

 

<td><input type="radio" name="votes['.$photo_id.']'. $photo_id. '" id="vote1" value="1" /></td>'.PHP_EOL;
...

 

Which when used in a loop, will give me a votes array with two values, the photo_id and the rating given, to use your photo_id values:

 

votes('12'=>2, '67'=>3, '82'=>7)

 

After your post, I figured the extra photo_id that i have put in is redundant!!

 

Thanks alot mate!!

 

 

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.