dynamic readio button loop - assigning name and passing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
btm
Forum Newbie
Posts: 11
Joined: Wed Sep 14, 2005 7:21 am

dynamic readio button loop - assigning name and passing

Post by btm »

Hi!
So I'm creating an online form that pulls information from a database and inserts part of it into a series of radio buttons. The issue is that in order for the radio buttons to function independent of each other, each one needs a unique name. I've suceeded in creating an incrementing variable that I attach to the beginning of each radio button as it goes through the loop, but I can't figure out how to get the data to the next page or into the database...
Code:

Code: Select all

while($row = mysql_fetch_assoc($result))
	{   //this loops out the data into multiple columns	 

		print("<td align='left' style='padding: 3px;'>"); 
		print("<textarea name='description[]' rows='2' cols='20'>$row[description]</textarea><br /> \n");
		
		if(trim($row[success_flag]) == "success")
		{
			echo ('<input type="radio" name="success_fail[]'.$i.'" value="'.$i.'success" CHECKED/> Success <br>');
			echo ('<input type="radio" name="success_fail[]'.$i.'" value="'.$i.'failure"/> Failure <br>');
			print("</td> \n\n");						
			$i++;
		}
		else if(trim($row[success_flag]) == "failure")
		{
			
			echo ('<input type="radio" name="success_fail[]'.$i.'" value="'.$i.'success"/> Success <br>');
			echo ('<input type="radio" name="success_fail[]'.$i.'" value="'.$i.'failure" CHECKED/> Failure <br>');			
			print("</td> \n\n");
						
			$i++;

		}
		else
		{
			echo ('<input type="radio" name="success_fail[]'.$i.'" value="'.$i.'success"/> Success <br>');
			echo ('<input type="radio" name="success_fail[]'.$i.'" value="'.$i.'failure"/> Failure <br>');
			print("</td> \n\n");
			
			$i++;
		}

	}
This comes out in the page source as:

Code: Select all

<input type="radio" name="success_fail[]1" value="1success" CHECKED/> Success <br>
<input type="radio" name="success_fail[]1" value="1failure"/> Failure <br></td> 

...code...

<input type="radio" name="success_fail[]2" value="2success" CHECKED/> Success <br>
<input type="radio" name="success_fail[]2" value="2failure"/> Failure <br></td> 


<input type="radio" name="success_fail[]3" value="3success"/> Success <br>
<input type="radio" name="success_fail[]3" value="3failure" CHECKED/> Failure <br></td>
I'm basically trying to figure out what the variable that gets passed looks like and how to play with it (at least print each passed value out to begin with).

Anyone have any ideas? Thanks :P
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Run this example and you will know wot you should do.

Code: Select all

<form method='get'>
<?php for($i=0; $i < 3; $i++){ ?>
	<?php  echo "Radion button set:$i"; ?><br />
	<input type="radio" name="success_fail[<?php echo $i; ?>][]" value="success" CHECKED/> Success <br> 
	<input type="radio" name="success_fail[<?php echo $i; ?>][]" value="failure"/> Failure <br>
<?php }?>
<input type = 'button' value = 'submit' onclick = 'form.submit();' />
</form>


<pre>
<?
if (isset($_GET) && is_array($_GET)){
	print_r($_GET);
}
?>
</pre>
btm
Forum Newbie
Posts: 11
Joined: Wed Sep 14, 2005 7:21 am

Thanks

Post by btm »

Perfect!

Thanks.
Post Reply