www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35

Thread: need help...please...

  1. #1
    Join Date
    Dec 2005
    Posts
    23

    need help...please...

    i am a newbie in web developing... i'm trying to create an online quiz... i am using mysql and php... i was able to display the questions and choices (multiple choice using radiobuttons) generated from the database... however, i am having a hard time getting the values of the radiobuttons selected so that i can compare them with the right answers then compute for the score... i really need your help guys... anything will be appreciated... i am really lost now... please...

  2. #2
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    34
    You wouldnt like to post some code would you? use sessions when you set hte answers
    New Zealand, Its beautiful.

  3. #3
    Join Date
    Jun 2004
    Location
    4846′36″ N 910′48″ E
    Posts
    3,747
    and use checkboxes for multiple choice, otherwise you'll confuse the user!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,146
    Quote Originally Posted by LiLcRaZyFuZzY
    and use checkboxes for multiple choice, otherwise you'll confuse the user!
    Hmmm?????

    For most multiple-choice questions you're only supposed to select one of the choices, so radio buttons would seem to be the correct implementation over checkboxes.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Dec 2005
    Posts
    23

    Unhappy

    Quote Originally Posted by NewZealand
    You wouldnt like to post some code would you? use sessions when you set hte answers
    forgive me for my stupidity... but can you please explain that further... i've been trying to use this code... but i really don't know where to go from here.. i was able to get the expected outcome, if and only if, i only have one record on the database... but as soon as i add more records on it... it only displays the value of the last radiobutton i clicked... any help will still be appreciated...
    PHP Code:
    <html>
    <head>
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    $connection 
    mysql_connect("localhost","user","password") or die("Couldn't connect to server.");
    $db mysql_select_db("database",$connection) or die("Couldn't connect to database.");
    $sql "SELECT * FROM table ORDER BY id";
    $sql_result mysql_query($sql,$connection) or die("Couldn't execute query.");
    if (
    $sql_result)
    {
        while (
    $row mysql_fetch_array($sql_result))
        {
            
    $id $row["id"];
            
    $question $row["question"];
            
    $opt1 $row["opt1"];
            
    $opt2 $row["opt2"];
            
    $opt3 $row["opt3"];
            
    $opt4 $row["opt4"];
            echo 
    "<b>$question</b><br />";
            echo 
    "<form method=\"post\" action=\"try2.php\">";
            echo 
    "<input type=\"radio\" name=\"option\" value=\"$opt1\">$opt1 <br /><input type=\"radio\" name=\"option\" value=\"$opt2\">$opt2 <br /><input type=\"radio\" name=\"option\" value=\"$opt3\">$opt3 <br /><input type=\"radio\" name=\"option\" value=\"$opt4\">$opt4 <br />";
        }
        echo 
    "<input type=\"submit\" value=\"Get My Scores\" name=\"submit\">";
        echo 
    "</form>";
    }
    ?>
    </body>
    </html>
    Last edited by NogDog; 12-12-2005 at 02:41 AM. Reason: Added PHP tags

  6. #6
    Join Date
    Jun 2004
    Location
    4846′36″ N 910′48″ E
    Posts
    3,747
    Quote Originally Posted by NogDog
    Hmmm?????

    For most multiple-choice questions you're only supposed to select one of the choices, so radio buttons would seem to be the correct implementation over checkboxes.
    oh yeah sorry, i thought you meant multiple selection, sorry!

  7. #7
    Join Date
    Dec 2005
    Posts
    23

    Unhappy

    i thought of something... however, i am not 100% sure about it... i tried using a for() statement for the name of my radiobuttons... so that each set of radiobuttons will have a distinct name... but my problem now is... how do i use $_POST to get the clicked radiobutton's value... here is the sample code...

    for($i = 0; $i < $num_rows; $i++)
    {
    echo "$i<br />";
    echo "<input type=\"radio\" name=\"$i\" value=\"$i\">sample1<br />";
    echo "<input type=\"radio\" name=\"$i\" value=\"$i\">sample2<br />";
    }

    still need your help...
    Last edited by am_a_newbie; 12-12-2005 at 09:23 PM.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,146
    If you want a set of radio buttons to be mutually exclusive, i.e. only one of them can be selected, then they need to each have the same name. When you access that name in the $_POST array, you'll get the value of the one which was selected.

    I would set up the form like this, using the question ID as the name for each set of radio buttons, so that you can use that to reference back to the DB to check the answer:
    PHP Code:
    <?php
    $connection 
    mysql_connect("localhost","user","password") or die("Couldn't connect to server.");
    $db mysql_select_db("database",$connection) or die("Couldn't connect to database.");
    $sql "SELECT * FROM table ORDER BY id";
    $sql_result mysql_query($sql,$connection) or die("Couldn't execute query.");
    if (
    $sql_result and mysql_num_rows($sql_result) > 0)
    {
      echo 
    "<form method='post' action='try2.php'><div>";
      while (
    $row mysql_fetch_assoc($sql_result))
      {
        
    $id $row["id"];
        
    $question $row["question"];
        
    $opt1 $row["opt1"];
        
    $opt2 $row["opt2"];
        
    $opt3 $row["opt3"];
        
    $opt4 $row["opt4"];
        echo 
    "<h3>$question</h3>\n";
        foreach(array(
    $opt1$opt2$opt3$opt4) as $value)
        {
          echo 
    "<input type='radio' name='$id' value='$value'>$value <br />";
        }
      }
      echo 
    "<input type='submit' value='Get My Scores' name='submit'>";
      echo 
    "</div></form>";   
    }
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Dec 2005
    Posts
    23
    thanks for the reply... i just have another question... how can i retrieve the value selected using $_POST if i have another variable for its value... i mean... for example, if i wanted to print out the value of the selected radiobutton can i do it this way...

    $id = $_POST['id'];
    echo "$id";

  10. #10
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    34
    PHP Code:
    //either
    $id $_POST['id'];
    echo 
    $id;

    //or plain old
    echo $_POST['id']; 
    New Zealand, Its beautiful.

  11. #11
    Join Date
    Dec 2005
    Posts
    23
    i've actually tried doing those but i just get a blank page... do you think there's something else i need to add since the name of my radiobutton is another variable... do you think i have to do it this way...

    $id = $_POST['$id'];

  12. #12
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    34
    I see why, because the id code is different every time, it will be the result from the DB,

    You need to echo all the id's from the post with a for each statement
    New Zealand, Its beautiful.

  13. #13
    Join Date
    Dec 2005
    Posts
    23
    i hope it's not too much if i ask you for a sample code... i'm really new at this... i'm sorry...

  14. #14
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    34
    I'm not too crash hot on this subject but I think you areworking with something like

    PHP Code:
    foreach ($_POST as $key => $value) { 
    ************echo 
    "id =" .$key ." : "$value "\n"
    ********} 
    http://php.net/foreach
    New Zealand, Its beautiful.

  15. #15
    Join Date
    Dec 2005
    Posts
    23
    thank you so much for the reply... i'll try to work on the code you have posted... but i won't promise that i won't be coming back to ask other questions... you know... in case i get lost again... hehehe... hope you won't be mad at me and would still reply to my posts... i'm pretty sure i still have a lot of things to learn about php...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles