473,480 Members | 1,777 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

append a loop variable in a radio button name

7 New Member
I used the for loop variable $i to each radio button so that each radio button will have a unique name. the problem now is how can i display the values selected in the radio button on the same page as where the form is located.
your help is greatly appreciated.. thanks
here's the code:
NOTE: this code is cut short, i only posted the part where i got stuck
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.         if(isset($_GET['sub'])){
  3.             $num = $_GET['num'];
  4.             $num = 2;
  5.             for($i=1; $i<=$num; $i++ ){
  6.                 echo '
  7.                 <tr>
  8.                     <td align="center">
  9.                         Question no.: '.$i.'
  10.                         <br>
  11.                         A.<input type="radio" name="'.$i.'[]"   
  12. value="A"><br>
  13.                         B.<input type="radio" name="'.$i.'[]" value="B"><br>
  14.                         C.<input type="radio" name="'.$i.'[]" value="C"><br>
  15.                         D.<input type="radio" name="'.$i.'[]" value="D"><br>
  16.                     </td>
  17.                 </tr>';
  18.             }
  19.         }
  20.         if($_GET['eva']){
  21.             $choice = $_GET['$i']; //this is where i got stuck
  22.             foreach($choice as $opt){
  23.                 echo $opt;
  24.             }
  25.         }
  26.     ?>
  27.     <tr>
  28.         <td align="center">
  29.             <input type="submit" name="eva" value="evaluate">
  30.         </td>
  31.     </tr>
Feb 2 '08 #1
8 6747
harshmaul
490 Recognized Expert Contributor
Hi,
A few problems with yout mark up....
the radio buttons need to be in a group with the SAME name. the whole thing about radio buttons is that they are grouped together, (eg. male female, single married). if you want them to be selected individualy you will need to use a "checkbox".

But back to your problem so having given your radio buttons a static name you can get it from the forms method variable but only on the post back.

If your like me i can understand code better than conversation so here is some code....

The HTML
Expand|Select|Wrap|Line Numbers
  1. A.<input type="radio" name="'myradioButton" value="A">A<br>
  2. B.<input type="radio" name="'myradioButton" value="B">B<br>
  3. C.<input type="radio" name="'myradioButton" value="C">C<br>
The PHP
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (IsSet($_GET("myradioButton")){
  3. ?>
  4. the value of the radio buttons was....<?php echo $_GET("myradioButton"); ?>
  5. <?php } ?>
Feb 2 '08 #2
romano2717
7 New Member
Hi,
A few problems with yout mark up....
the radio buttons need to be in a group with the SAME name. the whole thing about radio buttons is that they are grouped together, (eg. male female, single married). if you want them to be selected individualy you will need to use a "checkbox".

But back to your problem so having given your radio buttons a static name you can get it from the forms method variable but only on the post back.

If your like me i can understand code better than conversation so here is some code....

The HTML
Expand|Select|Wrap|Line Numbers
  1. A.<input type="radio" name="'myradioButton" value="A">A<br>
  2. B.<input type="radio" name="'myradioButton" value="B">B<br>
  3. C.<input type="radio" name="'myradioButton" value="C">C<br>
The PHP
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (IsSet($_GET("myradioButton")){
  3. ?>
  4. the value of the radio buttons was....<?php echo $_GET("myradioButton"); ?>
  5. <?php } ?>

the problem is i dont know how to get the values of the selected radio buttons since the name of each radio buttons has a variable appended to it. can anyone help me with this?
Feb 3 '08 #3
Markus
6,050 Recognized Expert Expert
Well don't give a unique name to the input.

name="something[]"

Will do just fine.
Feb 3 '08 #4
romano2717
7 New Member
Well don't give a unique name to the input.

name="something[]"

Will do just fine.
but i need to have a unique name for each set of radio buttons, so for every loop it will create four radio buttons with unique names.
Feb 5 '08 #5
harshmaul
490 Recognized Expert Contributor
Hi,
are the radio button mutually exclusive to eachother? or do you want them grouped together?
Feb 5 '08 #6
romano2717
7 New Member
Hi,
are the radio button mutually exclusive to eachother? or do you want them grouped together?
i want them grouped together so i will have a set of radio buttons consist of four radio buttons.. imagine an exam.. you have 5 questions with 4 choices each, so each question which is consist of 4 choices you can only select one radio button each question.
Feb 6 '08 #7
harshmaul
490 Recognized Expert Contributor
Hi,
Refer to my previous post.

All the ones in the same "answer" for the question will need the same name like question1, and the value between the tags will be the value displayed and posted back to the server. if you post all relevent code i will show you.
Feb 6 '08 #8
i have the same problems. is there any solutions? thanks
Sep 27 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3244
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
33
3621
by: Brian | last post by:
I have a list of plain HTML radio buttons that I want to be able to loop through, get the values from them and insert them into a db. each one should be a separate record... Can anyone please give...
3
3834
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
6
1655
by: Ed Jay | last post by:
<disclaimer>New to js.</disclaimer> I have several pages, each with menues comprising checkboxes or radio boxes within the same form. I presently 'brute force' clear the buttons with individual...
2
3390
by: Mirovk | last post by:
Hi, I have a session variable wich value arrives from a previous .asp but in my actual page I need to modify it based upon a selected value from a radio button. I will try to figure out for...
9
9295
by: wreed | last post by:
I have a for loop seen below.... var the_form = document.getElementById(formName); for(var i=0; i<the_form.length; i++) { var temp = the_form.elements.type; if (temp == "radio") { for (x =...
9
2214
by: IchBin | last post by:
I can not see what the problem is with this script. I am just trying to set a radio button by calling setCheckedValue('abbr_letter', 'V'). Sorry I am new to javascript. <html> <head> <script...
10
6044
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
2
21357
by: somacore | last post by:
VS2005, C# Windows Form I can't quite figure out how to do this, but i think I'm close. I have a groupbox which contains 8 radio buttons, and I need to figure out which one is checked for entry...
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.