Jump to content


Photo

handling a radio button name(array) from a form


  • Please log in to reply
11 replies to this topic

#1 imohiddin

imohiddin
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 May 2006 - 12:24 PM

hi,

im making a website for my final year project, and im having some trouble. im making a voting site that uses the single transferable vote method. im new to php as well.
basically, im having trouble handling radio buttons made in another script. the problem i have is that i've set the name of each set of radion button as the name of the candidate, and this is done by using a for loop and an array( ass you can see in the code below)

echo '<form action="registervote.php" method="post">';
echo '<table align="center" cellspacing="50" cellpadding="5" border="2">';
echo '<tr>';
for($i=1; $i<=$num; $i++)
{
    $arr1=mysql_fetch_array($result, MYSQL_ASSOC);
    echo '<th align="center" bgcolor="yellow">' . $arr1['name'] . '</th>';

    while(!$i)
    {
       echo '</tr><tr>';
    }

    for($j=0; $j<=$num; $j++)
   {
  echo '<td align="center">'.($j+1).'<input type="radio" name="'.$arr1['name'].'" value="'($j+1).'"/></td>';
   }
  echo '</tr>';
   }

 echo '</table>';

echo '<p><br /><center/><input type="submit" name="submit" value="VOTE!" /></p>';
 echo '<input type="hidden" name="submitted" value="TRUE" />';

the handling script is below, what i need it to do is to confirm that an input has been selected(then i can add the input value into my MySQL database table):

if(isset($_POST['submitted']))
{

    if(isset($_POST['']))
    {
        echo '<p>input has been selected</p>';
    }
    else
        {
            echo 'no input';
        }

}

what i haven't been able to figure out for two days is what i should write in the $_POST[] array to handle the radio button. all the radio buttons would have their names set as the candidate name in the ballot that they would be taking part in. so i cant just set each set of radio buttons' name as the name of each candidate manually, as these can change for each ballot. i dont know how to check if the name of the radio button(which is an array) has been set, using the isset() function. please help.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 01 May 2006 - 01:50 PM

I'm not sure you understand how radio buttons work:

1) If you have a group of radio buttons, they all have the same name.
2) The value of the one that is selected will be passed as the result.

<input type="radio" name="asdf" value="1">blah
<input type="radio" name="asdf" value="2">blah
<input type="radio" name="asdf" value="3">blah
<input type="radio" name="asdf" value="4">blah

<input type="radio" name="lkjh" value="1">blah
<input type="radio" name="lkjh" value="2">blah
<input type="radio" name="lkjh" value="3">blah
<input type="radio" name="lkjh" value="4">blah

Then in the post, you'll have $_POST['asdf'] and $_POST['lkjh'] with a value between 1 and 4.

If that doesn't clear it up, please rephrase your question.

I think your problem is that you're putting the array value as the name instead of the value.

Info: PHP Manual


#3 imohiddin

imohiddin
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 May 2006 - 02:38 PM

thank you for your reply ober,

sorry i didnt make clear my question. basically, i have a few candidates in a ballot. so each candidate will have his own group of radio buttons. the name of each candidate's group will be set as his/her name but the values will be different as you can see below and in the html source i've also included.

<input type="radio" name="Wayne Rooney" value="1"/>
<input type="radio" name="Wayne Rooney" value="2"/>
<input type="radio" name="Wayne Rooney" value="3"/>
<input type="radio" name="Wayne Rooney" value="4"/>

the voter will select a number(for this example, 1 to 4). so what im having trouble with is what to write in the $_POST, so that the script checks which candidate's radio button has been selected.
i can't manually set the name of each candidate because each ballot will have different candidates, so the names would be different and thats the reason i have set the name of each candidate in the for loop as:

name="'.$arr1['name'].'"

$arr1 is defined as:

$arr1=mysql_fetch_array($result, MYSQL_ASSOC);

so the name of each radio button will be the name of each candidate because $result is set thus:

$query="SELECT CONCAT(candidate_Fname, ' ', candidate_Sname) AS name FROM candidates";
$result=@mysql_query($query);

also $num is:

$num=mysql_num_rows($result);


below is the html source for the page:

<form action="registervote.php" method="post"><table align="center" cellspacing="50" cellpadding="5" border="2"><tr><th align="center" bgcolor="yellow">Wayne Rooney</th><td align="center">1<input type="radio" name="Wayne Rooney" value="1"/></td><td align="center">2<input type="radio" name="Wayne Rooney" value="2"/></td><td align="center">3<input type="radio" name="Wayne Rooney" value="3"/></td><td align="center">4<input type="radio" name="Wayne Rooney" value="4"/></td></tr><th align="center" bgcolor="yellow">John Smith</th><td align="center">1<input type="radio" name="John Smith" value="1"/></td><td align="center">2<input type="radio" name="John Smith" value="2"/></td><td align="center">3<input type="radio" name="John Smith" value="3"/></td><td align="center">4<input type="radio" name="John Smith" value="4"/></td></tr><th align="center" bgcolor="yellow">Eric Cantona</th><td align="center">1<input type="radio" name="Eric Cantona" value="1"/></td><td align="center">2<input type="radio" name="Eric Cantona" value="2"/></td><td align="center">3<input type="radio" name="Eric Cantona" value="3"/></td><td align="center">4<input type="radio" name="Eric Cantona" value="4"/></td></tr><th align="center" bgcolor="yellow">Cristiano Ronaldo</th><td align="center">1<input type="radio" name="Cristiano Ronaldo" value="1"/></td><td align="center">2<input type="radio" name="Cristiano Ronaldo" value="2"/></td><td align="center">3<input type="radio" name="Cristiano Ronaldo" value="3"/></td><td align="center">4<input type="radio" name="Cristiano Ronaldo" value="4"/></td></tr></table><p><br /><center/><input type="submit" name="submit" value="VOTE!" /></p><input type="hidden" name="submitted" value="TRUE" />

i hope that i've made it clearer now and apologies for how long this is. i look forward to your reply.thnx again.

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 01 May 2006 - 02:55 PM

Alright... that makes a little more sense.

However, might I suggest a slight modification? I assume you have IDs associated with each candidate, correct? I'd suggest using the IDs as the names instead of the candidate names because you're going to run into issues with spaces being converted in the URL and so on and so on.

Beyond that, I also suggest the following:

Name your radio button groups like this:
<input type="radio" name="candidate_'.$arr1['ID'].'" value="1"/>

Then in your processing, use a simple foreach loop to pull those out and manage them seperate from your other form elements (if there are any):
foreach($_POST as $key => $val)
{
     if(substr($key,0,10) == 'candidate_')
     {
          $id = substr($key,11);
          // run query to get name from ID
          echo "candidate x has been given a rating of $val";
     }
}

Does that help?

Info: PHP Manual


#5 imohiddin

imohiddin
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 May 2006 - 06:28 PM

thank you for your reply once again ober,

i really appreciate your suggestion and it has helped so far. i am however having some issues again(you can see im a beginner). this time it is with the printing of the candidates' names where you have suggested

echo "candidate x has been given a rating of $val";

i am having trouble getting it to print the names of each candidate where you have written 'candidate x'.

this is the code that i'm trying to write:

if(isset($_POST['submitted']))
{

   foreach($_POST as $key => $val)
   {
      if(substr($key,0,10) == 'candidate_')
      {
          $id = substr($key,11);
          require_once('./mysql_connectevote1.php');
         $query="SELECT CONCAT(candidate_Fname, ' ', candidate_Sname) AS name FROM candidates WHERE candidateID='$id'";
            
           $result=mysql_query($query);
          
            while($row=mysql_fetch_array($result, MYSQL_ASSOC))
            {
    //run query to get name from ID
    echo "".$row['name']." has been given a rating of $val <br/>";

            }

       }
    }

}

$row['name'] keeps printing the name of another candidate(who will be in another ballot) instead of printing the name of each candidate in this ballot. i think its to do with loops or something.
your help and suggestions/modifications is greatly appreciated in this.

#6 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 01 May 2006 - 07:29 PM

My math may be a little off.

Try changing this:
$id = substr($key,11);

To this:
$id = substr($key,10);

Or just play around with that to make sure you're getting the entire ID out of the POST variable. Also, you can remove the while loop since you should only have to call that mysql_fetch line once.

Info: PHP Manual


#7 imohiddin

imohiddin
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 May 2006 - 08:12 PM

thnx for your latest reply,

i think that i haven't made myself clear again. my question is how can i make it give me something like:

" Wayne Rooney has been given a rating of 1"

instead of:

" candidate x has been given a rating of 1"

at the moment it is giving me the value of $val, but not the candidates' name.
if i need to write another mysql query, do u think that u could tell me what i need to write please?

thanx for your help.

sorry mate,

it's working now! i didnt follow your instructions properly. i changed it to $id=($key, 10) and its working now.

i really appreciate all the help you've given me and i couldn't have got on with my work without your help.

thank you again.

ps:- if i have more problems with this project should i post it in this thread or another?

#8 imohiddin

imohiddin
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 May 2006 - 09:10 PM

sorry again,

just (hopefully) one last thing...how can i stop voters ranking two(or more) candidates with the same ranking. e.g. giving both 'wayne rooney' and 'eric cantona' a ranking of 1.
can i do this at the form level or does it have to be done in the form-handling script?

thanks once more.

#9 imohiddin

imohiddin
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 02 May 2006 - 02:19 AM

can anybody help? i think ober might not be available at the moment and i need this quite urgently.

thanks every1.

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 May 2006 - 04:00 AM

A few months ago, someone else asked a similar question and I came up with the following code:
<?php
$err_style = array();
$radio_sylye = array();
if (isset($_POST['submit'])) {
    $unique_opts = array_unique($_POST['opt']);
    for ($i=1;$i<17;$i++)
        if (!isset($unique_opts[$i])) {
            $err_style[$i] = ' style="color:red;font-weight:bold;"';
            $radio_style[$i] = ' style="background-color: red';
        }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Ranking Test</title>
    <style type="text/css" media="screen">
    .row {
        clear:both;
        display:block;
        height:1.5em;
        width:100%;
    }
    .label {
        display:block;
        float:left;
        width:5em;
    }
    .header {
        float:left;
        display:block;
        height:1.5em;
        width:2em;
        text-decoration:underline;
        font-weight:bold;
    }
    .radiob {
        float:left;
        display:block;
        height:1.5em;
        width:2em;
    }
    .sub {
        clear:both;
        float:left;
        display:block;
        margin-top:0.25em;
    }
    .red {
        color: Red;
    }
    
    .bold {
        font-weight: bold;
    }
    
    hr {
        clear: both;
    }
    </style>
</head>

<body>
<form method="post">
<div class="row"><span class="label">&nbsp;</span>
<?php
for ($i=1;$i<17;$i++)
    echo '<span class="header">' . $i . '</span>';
echo '</div>'."\n";
for ($row = 1; $row < 17; $row++) {
    echo '<div class="row"><span class="label"';
    if (isset($err_style[$row])) echo $err_style[$row];
    echo '>Item #' . $row . ':</span>';
    for ($opt = 1; $opt < 17; $opt++) {
        echo '<span class="radiob"';
        if (isset($_POST['opt'][$row]) && $_POST['opt'][$row] == $opt && isset($radio_style[$row])) echo $radio_style[$row];
        echo '">';
        echo '<input type="radio" id="opt_' . $row . '_' . $opt . '" name="opt[' . $row . ']" value="' . $opt . '"';
        if (isset($_POST['opt'][$row]) && $_POST['opt'][$row] == $opt) {
            echo ' checked="checked"';
        }
        echo '></span>'; }
    echo "</div>\n";}
?>
<div class="sub"><input type="submit" name="submit" value="Submit Rankings"></div>
</form>
<?php
if (isset($_POST['submit'])) {
    echo '<hr>';
    if (count($_POST['opt']) != 16) echo "<p class='red'>You didn't rank all of the items, try again</p>\n";
    else {
        $unique_opts = array_unique($_POST['opt']);
        if (count($unique_opts) != 16) {// duplicate ranking found
            $tmp = array();
            for ($i=1;$i<17;$i++){
                if (!isset($unique_opts[$i])) $tmp[] = $i.'<span class="bold">(Rank: ' . $_POST['opt'][$i] . ')</span>';
            }
            echo 'The rankings of the following Items were already used: ' . implode(', ',$tmp) . '<br>';
        }
    }
}
?>
</body>
</html>

Look at the code -- see it in action at <[a href=\"http://www.rbnsn.com/phpfreaks/ranking.php\" target=\"_blank\"]http://www.rbnsn.com/phpfreaks/ranking.php[/a]>

Have fun... :-)

Ken

#11 imohiddin

imohiddin
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 02 May 2006 - 03:15 PM

thanks for your reply ken,

its just the kind of thing i was looking for. im gonna have a look and try to implement it into my site. however, do you think that you could have a look at my coding(from previous threads) and see how i can make the rankings not the same for two or more candidates.

your help is appreciated.

#12 imohiddin

imohiddin
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 03 May 2006 - 12:10 AM

hey guys,

anybody know how i should change my coding(see above) to make my script act like ken's one? i.e. no ranking can be repeated for two or more candidates.
please take into account my coding so far and that i am using it with mysql where i have a table named 'votes' with columns voter_id, candidateID, election, rank...where election is the election ballot that the candidate will be taking part in.

cheers




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users