PHP form - issues with radio buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Franky
    New Member
    • Mar 2007
    • 14

    PHP form - issues with radio buttons

    I am trying to create a form to be used for testing during a tutorial (at the end of each module the user will be given a test).

    It contains a number of questions, each with a question and 4 possible answers. The answers are radio buttons, so the user can only select 1 for each question.

    I am currently selecting 5 questions randomly from a database and displaying them using a loop:

    $a = 1;
    while ($row = mysql_fetch_ass oc($result)){ //$result is the question data from the db
    <TABLE>
    <TR><TD><b><? echo "$a. ", $row ['Question']. "<br/>" ?></b></TD></TR>
    <TR><TD><inpu t type="radio" name="Question$ a" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
    <TR><TD><inpu t type="radio" name="Question$ a" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
    <TR><TD><inpu t type="radio" name="Question$ a" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
    <TR><TD><inpu t type="radio" name="Question$ a" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
    </TABLE>
    <?php
    print "<br/>";
    $a++;
    }//end while

    I have used name="Question$ a" so that each answer set will have a different name (i.e. Question1, 2,...) so i can then use $answer1 = $_POST ['Question1'] later to get the user's answer for each question. This isn't working though.

    Firstly, it only allows me to select one radio button on the whole page (rather than one per question) and it is not giving each set a different name (the name="Question$ a" bit).

    I have tried other options but am really struggling now. Any help will be much appreciated.
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Try to understand what i have done here. and replace your $result String for the while loop.

    [PHP]<?php
    $a = 1;
    while ($a<=5)
    {
    ?>
    <TABLE>
    <TR><TD><b><? echo "$a. ", $row ['Question']. "<br/>" ?></b></TD></TR>
    <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
    <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
    <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
    <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
    </TABLE>
    <?php
    print "<br/>";
    $a++;
    }
    ?> [/PHP]

    Comment

    • Franky
      New Member
      • Mar 2007
      • 14

      #3
      Thanks for the help, I've used that code and it solves the two issues. However, it outputs the same question for each (i.e. it isn't cycling through the query result which is an array containing the 5 questions and options). How do i get it to do this?

      thanks again.

      Comment

      • Franky
        New Member
        • Mar 2007
        • 14

        #4
        No worries, got it to work. I used my original code with the change you made to the name="" section of the radio buttons. So it now has name="<?php print 'Question'.$a ?>" instead.

        It seems to work fine, but if you see any issues with this let me know.

        Thanks for the help.

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          [PHP]<?php
          include 'dbcon.php';
          $sql = 'select * from products LIMIT 5';// You have to create a SQL Query as per your Reqirement
          $result = mysql_query($sq l);
          $a = 1;
          while ($row = mysql_fetch_ass oc($result))
          {
          ?>
          <TABLE>
          <TR><TD><b><? echo "$a. ", $row['p_id']. "<br/>" ?></b></TD></TR>
          <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
          <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
          <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
          <TR><TD><inpu t type="radio" name="<?php print 'Question'.$a ?>" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
          </TABLE>
          <?php
          print "<br/>";
          $a++;
          }
          ?> [/PHP]

          Comment

          Working...