How to use while loop with radio button

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
skmdas
Forum Newbie
Posts: 5
Joined: Fri Nov 21, 2008 9:56 am

How to use while loop with radio button

Post by skmdas »

Hi,

I am totally new to php. can someone help me about how to use while loop with radio button and insert the values to table when submitted.

Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to use while loop with radio button

Post by pickle »

A radio button will normally only occur once in $_POST. What are you 'looping' through? Perhaps a little more information?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: How to use while loop with radio button

Post by aceconcepts »

@skmdas: if you're looping data retrieved from a database and which to create checkboxes that are POSTED as arrays then append the checkbox name with "[]".

Obviously when you validate the POSTED checkbox values you'll need to setup an additional loop, most suitable (i would imagine) is a for loop.
skmdas
Forum Newbie
Posts: 5
Joined: Fri Nov 21, 2008 9:56 am

Re: How to use while loop with radio button

Post by skmdas »

Hi,

The one which i am working is a questionnaire. It has 15 to 20 questions and 4 choices for each question. the four choices is radio button ffrom that for each question one choice has to be selected. All the questions will be dispalyed in the same screen. once all the questions are answered you press the submit key and the question number and the answer is inserted into a table and based on that the score will be displayed.

Hope this helps you

Thanks
sree
EPX
Forum Newbie
Posts: 19
Joined: Fri Nov 21, 2008 3:22 am
Location: Stafford, UK

Re: How to use while loop with radio button

Post by EPX »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I think i can help, below is just an example that should be in the body of the page:


----------------------

Code: Select all

<form name="form1" method="post" action="page_name.php">
<input type="radio" name="question1" value="1">
<input type="radio" name="question1" value="2">
<input type="radio" name="question1" value="3">
<input type="radio" name="question1" value="4">
 
<input type="radio" name="question2" value="1">
<input type="radio" name="question2" value="2">
<input type="radio" name="question2" value="3">
<input type="radio" name="question2" value="4">
 
<!-- add more questions -->
 
<input type="submit" name="finish" value="Finish">
</form>
---------------------------------

Insert below into top of your page

--------------------------------

Code: Select all

<?php
if($_REQUEST['finish'])
{
$results = array();
#20 relates to number of questions
for($i=1;$i<=20;$i++)
{
 
$variable = "question".$i;
$results[$i] = $_REQUEST[$variable]; 
}
 
}
?>
---------------------------------------

To print the results just echo the result you want, example below:

--------------------------------------

Code: Select all

Question 1: <?php echo $results[1];?><br>
Question 2: <?php echo $results[2];?><br>
---------------------------------------

Hope thats want you were looking for. hope there isn't any mistakes


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: How to use while loop with radio button

Post by aceconcepts »

why not pass the checkboxes as array directly from form using "[]"?
Post Reply