kwdelre Posted January 11, 2011 Share Posted January 11, 2011 I am a newbie trying to build a quiz system. I have all of the questions and answer choices in a mysql table. My code below selects the question and answers. They appear exactly how I want them to....but I'm stuck on the form functionality. I was trying to use something like this: <input type='radio' name='Q1Choice' value='$answer1' /> I need to be able to insert the choice into my table. I know how to do the insert part but not sure how to take the radio choice as the insert data. <? //Connect to database mysql_connect($host, $dbusername, $password); mysql_select_db($TxQuizdb); //Retrieve Quiz Questions $qquery = mysql_query("SELECT * FROM Module_1_Quiz"); while ($m1questions = mysql_fetch_array($qquery)) { echo "<table width=600 border=1><tr><td>"; echo $m1questions['Question']; echo "</td></tr><br><tr><td>"; echo $m1questions['Choice1']; echo "<br>"; echo $m1questions['Choice2']; echo "<br>"; echo $m1questions['Choice3']; echo "</td></tr></table>"; } thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/ Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 so you need to know how to get the (i'm assuming) multiple choice options from the database, and make them appear next to a radio button?? Is this correct? It will be fairly simple if this is what you need.... Denno Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157751 Share on other sites More sharing options...
kwdelre Posted January 11, 2011 Author Share Posted January 11, 2011 Yeah I guess that's all i need, cause in the process file I will have their selection inserted into the table. I had it working before with the code below but that was when i had the quiz info in a php doc. echo '<ol type="a"><br><br>'; shuffle($q1ans); foreach($q1ans as $answer1) { echo "<li><input type='radio' name='Q1Choice' value='$answer1' />$answer1</li><br/>"; } echo '</ol>'; ?> <br /> <br /> <ol type="a"> <? //Begin Question #2 shuffle($q2ans); foreach($q2ans as $answer2) { echo "<li><input type='radio' name='Q2Choice' value='$answer2'/>$answer2</li><br/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157756 Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 So that doesn't work anymore? You've got the basic idea of what you need to do, is it just syntax somewhere? What doesn't work? What happens when you try to render the page with the code you've just posted? Denno Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157757 Share on other sites More sharing options...
kwdelre Posted January 11, 2011 Author Share Posted January 11, 2011 Yeah it did work fine but all of those variables used in it were from an array in a php file. Now I have moved all of the quiz contents to a database. When I run the following code it does not input anything into the fields for their answer choice. //Connect to database mysql_connect($host, $dbusername, $password); mysql_select_db($TxQuizdb); //Retrieve Quiz Questions $qquery = mysql_query("SELECT * FROM Module_1_Quiz"); while ($m1questions = mysql_fetch_array($qquery)) { echo "<table width=600 border=1><tr><td>"; echo $m1questions['Question']; echo "</td></tr><br><tr><td>"; echo "<input type='radio' name='Q1Choice' value='$answer1' />" echo $m1questions['Choice1']; echo "<br>"; echo "<input type='radio' name='Q1Choice' value='$answer1' />" echo $m1questions['Choice2']; echo "<br>"; echo "<input type='radio' name='Q1Choice' value='$answer1' />" echo $m1questions['Choice3']; echo "</td></tr></table>"; } Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157758 Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 I think something like this will work: //Connect to database mysql_connect($host, $dbusername, $password); mysql_select_db($TxQuizdb); //Retrieve Quiz Questions $qquery = mysql_query("SELECT * FROM Module_1_Quiz"); $quiz = "<table width=600 border=1>"; while ($row = mysqli_fetch_array($qquery)) { $question = $row["question"]; $answer1 = $row["answer_1"]; $answer2 = $row["answer_2"]; $answer3 = $row["answer_3"]; $quiz .= "<tr><td> $question"; $quiz .= "</td></tr><br><tr><td>"; $quiz .= "<input type='radio' name='Q1Choice' value='$answer1' />"; $quiz .= "$answer1"; $quiz .= "<input type='radio' name='Q1Choice' value='$answer2' />"; $quiz .= "$answer2"; $quiz .= "<input type='radio' name='Q1Choice' value='$answer3' />"; $quiz .= "$answer3"; $quiz .= "</td></tr>"; } $quiz .= "</table>" echo $quiz; The value in the square brackets next to $row is the name of each column with the question/possible answers in. ' .= ' means append data to the string, so it just adds it to the end. I've got the <table> starting tag outside of the loop, as this will put all of the information into the one table, instead of making multiple tables. I haven't tested the code above, so there is most definitely errors, just play around with it until you can get it to work, but that is the basic idea that you want. Denno Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157761 Share on other sites More sharing options...
kwdelre Posted January 11, 2011 Author Share Posted January 11, 2011 I see where you are going with that. Thanks a lot for your help man, it will at least get me trying seomthing I haven't. Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157765 Share on other sites More sharing options...
kwdelre Posted January 11, 2011 Author Share Posted January 11, 2011 I feel dumb... why can't I get the following to work? $quiz .= "<input type='radio' name='Q$iChoice' value='$answer1' />"; I am just using $i = 1 at the beginning of the loop and then $i=$i++ at the end. This way Q1Choice, Q2Choice, etc can be identified later. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157771 Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 $quiz .= "<input type='radio' name='Q" . "$i" . "Choice' value='$answer1' />"; You can try that, but the use of double quotes means that you can place variables into strings without having to escape and all that.. In what way doesn't it work? Does it give an error or anything? Denno Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157774 Share on other sites More sharing options...
kwdelre Posted January 11, 2011 Author Share Posted January 11, 2011 Well before trying to use $i it was only letting me select 1 radio for both questions being tested. So I figured it was the radio input name. Plus I would need the names to change anyway. When I put in the code with $i it still would only let me select one radio for both questions. It still does it with your adjustments. Here's where I'm at: mysql_connect($host, $dbusername, $password); mysql_select_db($TxQuizdb); //Retrieve Quiz Questions $qquery = mysql_query("SELECT * FROM Module_1_Quiz"); $quiz = "<table width=600 border=1>"; while ($m1questions = mysql_fetch_array($qquery)) { $question = $m1questions["Question"]; $answer1 = $m1questions["Choice1"]; $answer2 = $m1questions["Choice2"]; $answer3 = $m1questions["Choice3"]; $i = 1; $quiz .= "<tr><td> $question"; $quiz .= "</td></tr><br><tr><td>"; $quiz .= "<input type='radio' name='Q" . "$i" . "Choice' value='$answer1' />"; $quiz .= "$answer1"; $quiz .= "<input type='radio' name='Q" . "$i" . "Choice' value='$answer2' />"; $quiz .= "$answer2"; $quiz .= "<input type='radio' name='Q" . "$i" . "Choice' value='$answer3' />"; $quiz .= "$answer3"; $quiz .= "</td></tr>"; $i = $i++; } $quiz .= "</table>"; echo $quiz; Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157777 Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 I'm going to set up something myself so I can do some testing and I'll get back to you. Also, you don't have to do: $i = $i++l simply: $i++; will suffice Denno Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157778 Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 Alright I've got it to work. Here is the code that I'm using: <?php //connect to database require_once "connect_to_mysql.php"; $sqlCommand = "SELECT * FROM quiz"; $query = mysql_query($sqlCommand,$myConnection) or die (mysql_error()); $quiz = "<table width=600 border=1>"; $i = 0; while ($row = mysql_fetch_array($query)) { $i++; $question = $row["question"]; $answer1 = $row["answer1"]; $answer2 = $row["answer2"]; $answer3 = $row["answer3"]; $quiz .= "<tr><td> $question"; $quiz .= "</td></tr><br><tr><td>"; $quiz .= "<input type='radio' name='Q $i Choice' value='$answer1' />"; $quiz .= "$answer1"; $quiz .= "<input type='radio' name='Q $i Choice' value='$answer2' />"; $quiz .= "$answer2"; $quiz .= "<input type='radio' name='Q $i Choice' value='$answer3' />"; $quiz .= "$answer3"; $quiz .= "</td></tr>"; } $quiz .= "</table>"; mysql_free_result($query); ?> <!doctype html> <html> <head> <title>Test Page</title> <style type="text/css"> </style> </head> <body> <?php echo $quiz; ?> </body> </html> Give that a whirl and see what you come up with . Denno Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157782 Share on other sites More sharing options...
kwdelre Posted January 11, 2011 Author Share Posted January 11, 2011 Awesome, got it to work. Will pick up on it in the morning. Thanks a lot for your help man! Quote Link to comment https://forums.phpfreaks.com/topic/224049-form-functionality/#findComment-1157783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.