Page 1 of 1
How to use while loop with radio button
Posted: Fri Nov 21, 2008 10:03 am
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
Re: How to use while loop with radio button
Posted: Fri Nov 21, 2008 10:10 am
by pickle
A radio button will normally only occur once in $_POST. What are you 'looping' through? Perhaps a little more information?
Re: How to use while loop with radio button
Posted: Fri Nov 21, 2008 10:30 am
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.
Re: How to use while loop with radio button
Posted: Fri Nov 21, 2008 10:41 am
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
Re: How to use while loop with radio button
Posted: Fri Nov 21, 2008 1:18 pm
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:
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:
Posting Code in the Forums to learn how to do it too.
Re: How to use while loop with radio button
Posted: Sat Nov 22, 2008 7:27 am
by aceconcepts
why not pass the checkboxes as array directly from form using "[]"?