handling a radio button with array as name
Results 1 to 4 of 4

Thread: handling a radio button with array as name

  1. #1
    Junior Member
    Join Date
    May 2006
    Posts
    2

    handling a radio button with array as name

    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)

    Code:
    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):

    Code:
    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. #2
    Lost in Time (1970's) Houdini's Avatar
    Join Date
    Jul 2004
    Location
    Nashville TN USA
    Posts
    1,930
    radio button are supposed to be named with a common name but a different value e.g.
    HTML Code:
    <form name = 'myvoteform' action = 'someprocess.php' method = 'post'>
    <input type = 'radio' name ='candidate' value = 'democrat' />Democrat<br />
    <input type = 'radio' name ='candidate' value  = 'republican' />Republican<br />
    <input type = 'radio' name ='candidate' value = 'independant' />Independant<br />
    <input type = 'submit' value = 'Vote' />
    </form>
    Now you would have to amend your database so that you have fields for democrats, republicans, independants, and possibly other off the wall parties like the Green Party or the Marijuana Party or the Communist Party.

    Checkeboxs on the other hand should be an array since zero all or any number between zero and all can be selected. and would be written in the form as
    HTML Code:
    <input type = 'checkbox' name = 'foods[]' value = 'chicken' />Chicken<br />
    <input type = 'checkbox' name = 'foods[]' value = 'beef' />Beef<br />
    <input type = 'checkbox' name = 'foods[]' value = 'pork' />Pork<br />
    Or if you wanted to use an associative array then use
    HTML Code:
    <input type = 'checkbox' name = 'foods[chicken]' value = 'chicken' />Chicken<br />
    <input type = 'checkbox' name = 'foods[beef]' value = 'beef' />Beef<br />
    <input type = 'checkbox' name = 'foods[pork]' value = 'pork' />Pork<br />

  3. #3
    Junior Member
    Join Date
    May 2006
    Posts
    2
    thank you for your reply houdini,

    sorry i didnt make my question clearer. 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.

    HTML Code:
    <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:

    PHP Code:
    name="'.$arr1['name'].'" 
    $arr1 is defined as:

    PHP Code:
    $arr1=mysql_fetch_array($resultMYSQL_ASSOC); 
    so the name of each radio button will be the name of each candidate because $result is set thus:

    PHP Code:
    $query="SELECT CONCAT(candidate_Fname, ' ', candidate_Sname) AS name FROM candidates";
    $result=@mysql_query($query); 
    also $num is:

    PHP Code:
    $num=mysql_num_rows($result); 

    below is the html source for the page:

    HTML Code:
    <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. #4
    Lost in Time (1970's) Houdini's Avatar
    Join Date
    Jul 2004
    Location
    Nashville TN USA
    Posts
    1,930

    Names in form fields cannot contain spaces.

    I redid your form so the names will work and used a little formatiing so it is not all on one line.
    HTML Code:
    <html>
    	<body>
    	<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="hidden" name="submitted" value="TRUE" />
    			<input type="submit" name="submit" value="VOTE!" />
    	</form> 	
    		</p>
    	</body>
    </hyml>
    Then here is a simple check and assignment of variables you can chage to suit your form or database requirements.
    PHP Code:
    <?php 
    if(isset($_POST['Wayne_Rooney'])){
      
    $wayne $_POST['Wayne_Rooney'];
    }
    if(isset(
    $_POST['John_Smith'])){
      
    $john $_POST['John_Smith'];
    }
    if(isset(
    $_POST['Eric_Cantona'])){
      
    $eric $_POST['Eric_Cantona'];
    }
    if (isset(
    $_POST['Cristiano_Ronaldo'])){
      
    $cristiano $_POST['Cristiano_Ronaldo'];
    }
    echo 
    "\$_POST['Wayne_Rooney'] = ".$wayne."<br />";
    echo 
    "\$_POST['John_Smith'] = ".$john."<br />";
    echo 
    "\$_POST['Eric_Cantona'] = ".$eric."<br />";
    echo 
    "\$_POST['Cristiano_Ronaldo'] = ".$cristiano."<br />"
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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