Solution 1 :

Put the row ID in a hidden input. The you can use $_POST['id'] to know which question it’s the answer to.

while (counter !=0) {
    echo '
        <form method="post">

            <input type="radio" id="answerA-' . $row['id'] . '" name= "answer-' . $row['id'] . '" value="answerA" required> 
            <label for="answerA-' . $row['id'] . '"> ' . $row['answerA'] . ' </label><br>
            <input type="radio" id="answerB-' . $row['id'] . '" name="answer-' . $row['id'] . '" value="answerB"> 
            <label for="answerB-' . $row['id'] . '"> ' . $row['answerB'] . ' </label><br>
            <input type="radio" id="answerC-' . $row['id'] . '" name="answer-' . $row['id'] . '" value="answerC"> 
            <label for="answerC-' . $row['id'] . '"> ' . $row['answerC'] . ' </label><br>
            <input type="radio" id="answerD-' . $row['id'] . '" name="answer-' . $row['id'] . '" value="answerD">
            <label for="answerD-' . $row['id'] . '"> ' . $row['answerD'] . ' </label><br>
            <input type="hidden" name="id" value="' . $row['id'] . '">

        </form>
    '
    ;

}

There are other issues. IDs have to be unique, so you should include the question ID in the IDs of each of the answers.

All the names in a radio group have to be the same, that’s how the browser knows they’re alternative answers to the same question. But each form needs to have distinct radio groups. So you should take A, B, C and D out of the radio button names, but add in the question ID.

Problem :

I have code in a while loop that ultimately echoes out 30 questions. Since its iterating with the same code just different variable values, I cannot track a tag to use _POST with.

Is there anyway for me to store all the answers the user chooses while using a while loop?

code:

  while (counter !=0) {
        echo '
            <form method="post">

                <input type="radio" id="answerA" name= "answerA " value="answerA" required> 
                <label for="answerA"> ' . $row['answerA'] . ' </label><br>
                <input type="radio" id="answerB" name="answerB" value="answerB"> 
                <label for="answerB"> ' . $row['answerB'] . ' </label><br>
                <input type="radio" id="answerC" name="answerC" value="answerC"> 
                <label for="answerC"> ' . $row['answerC'] . ' </label><br>
                <input type="radio" id="answerD" name="answerD" value="answerD">
                <label for="answerD"> ' . $row['answerD'] . ' </label><br>


            </form>
        '
        ;

}

Comments

Comment posted by Barmar

Is there supposed to be code that reads a new row from the database? Otherwise you’re just printing the same answers every time.

Comment posted by Rushi M

they are different variables that are being echoed

Comment posted by Barmar

They’re all just different elements in the same

Comment posted by Rushi M

thank you so much for your help. I was just confused how this would exactly help me. normally i would do _POST(answerA) but since I am echoing it in a while loop, there are many answerAs. How does this hidden input help me call the specific answer the user inputs.

Comment posted by Barmar

You use

Comment posted by Rushi M

thank you so so much for your help. When I tried implementing it, it didn’t work. I used $counter instead of id btw. $ counter was initialized to 0.

Comment posted by Barmar

It needs to be something that relates to the database so you can match up the responses with the original data.

By