How to give a unique name to Radio buttons in a PHP while loop

    <div class=section><span> <?php echo $i; ?></span><?php $row["Ques"]?></div>
        <div class="inner-wrap">

<div id=radio-demo>
<input type='radio' name=<?php echo $i; ?>  id='1st-choice' value='4' checked/>
  <label for='1st-choice'><?php echo $row['1']; ?></label>
<input type='radio' name= <?php echo $i; ?>  id='2nd-choice' value='3' />
  <label for='2nd-choice'><?php echo $row['2']; ?></label>
   <input type='radio' name= <?php echo $i; ?>   id='3rd-choice' value='2' />
  <label for='3rd-choice'><?php echo $row['3']; ?></label>
   <input type='radio' name=<?php echo $i; ?>  id='4th-choice' value='1' />
  <label for='4th-choice'><?php echo $row['4']; ?></label>



This code Works perfectly I mean it show all the contents as required but I can make selection of radio button only for 1st question. If I try to select 2nd option from 2nd question even then 2nd option of 1st question is changed. I cannot make changes to any other question except 1st one

You can use your incremented number $i

<input type="radio" name="<?php echo $i; ?>"  id="1st-choice-q<?php echo $i; ?>" value="4" checked/>
  <label for="1st-choice-q<?php echo $i; ?>"><?php echo $row['1']; ?></label>

To output:-

<input type="radio" name="1"  id="1st-choice-q1" value="4" checked/>
  <label for="1st-choice-q1">Label Text</label>

if you check the code I have already done that but it didn’t work for me

Yes, you need to make sure the id is unique as SamA74 said, by adding the $i on the end. Same for the labels - if you present three selections each with the id “4th-choice”, which one would you assign the label to? But CSS rules say the ID field must be unique on a page.

I’m surprised it makes any difference to whether you can select the values though. Maybe the quotes make the difference there.

Also read around here for reasons to stop using the old-style mysql_() functions as they’re no longer a part of the language.

I don’t see that, you have:-




There is no variable, so it’s the same for every iteration of the loop.

Show us the code that processes the form submissions as well.

OUCH! if you give different names to a group it won’t serve the purpose.
Let me clear the scenario for you:
For 1st question

    <input type='radio' name="1"  id='2nd-choice' value='2' checked/>

For 2nd question
> >

>     <input type='radio' name="2"  id='2nd-choice' value='2' checked/>

So rather than writing these many questions I have used a while loop to iterate over all questions.
So obviously for 1st question radio button names must be true. For 2nd loop or iteration name should be same.

Yes, but every time you run around the loop, you present four items which all have the same id as the previous loop - so every first option has the id “1st-choice”, every second one is “2nd-choice” and so on. Not the “name”, the “id” and also the “label for”. You still have the “name” field the same for each group of four - otherwise as you say, the radio part won’t work - but you can’t put multiple IDs the same.

Show us the code you use to process the form.


Do you mean that the HTML page doesn’t react correctly - i.e. when you click option two on the second question it moves the selection in option one - or do you mean when you take the data into PHP and try to process it?

when you click option two on the second question it moves the selection in option one,
No matter whatever question we select changes are made only to 1st question

Yes, because the id and for attributes are the same. They must be unique like shown.

> <input type='radio' name=<?php echo $i; ?> value='<?php echo $row['1']; ?> value='4' checked/>

Now I have removed label and id but still doesn’t seem to work for me

Don’t remove them, make them unique.

1 Like

You’ll need the ID if you want the labels to work.

1 Like

Yes!!! This worked for me!!! Thank you to both of you

My trick might be helpful to any one who needs it later
id=<?php echo ($i*100+1); ?>
id=<?php echo ($i*100+2); ?>
id=<?php echo ($i*100+3); ?>
id=<?php echo ($i*100+4); ?>

Unless you really really need the IDs (for jQuery or whatever), you’ve overcomplicated this, as you don’t need an id for label to work. You simply wrap your input and label text with the label tag.


Also, see the HTML Spec (Example B)

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.