Hi Guys I am loading a test from a database, which contain all the questions and possible answers, Now what i want to do is display the possible answers as radio buttons and check if the user selected the correct radio button.(i will use the variable $cat_letter to check the value of the radio button selected.) I want to checked if each radio button selected is equal to $cat_letter and then add 1 to it if not add 0 and conitue with the while loop Can any of you guys be of help to me... <?php session_start(); include "db_connect.php"; $get_cats = "select * from tests1"; $get_cats_res = mysql_query($get_cats) or die(mysql_error()); if (mysql_num_rows($get_cats_res) < 1) { $display_block = "<P><em>Sorry, no categories to browse.</em></p>"; } else { $display_block .= " <table width='574' border='1' cellpadding='0' cellspacing='0' > <tr> <td> <h2>$cat_title </h2> </td> </tr>"; $hold[]; while ($cats = mysql_fetch_array($get_cats_res)) { $cat_id = $cats[id]; $cat_description = stripslashes($cats[description]); $cat_letter = stripslashes($cats[letter]); $cat_answer_a = stripslashes($cats[answer_a]); $cat_answer_b = stripslashes($cats[answer_b]); $cat_answer_c = stripslashes($cats[answer_c]); $display_block .= "<tr> <td width ='30' class='style17'>$cat_id <td/> <td align='center' width ='294' bgcolor='white' class='style19'>$cat_description<td/> <td width ='80'> $cat_answer_a <INPUT TYPE='radio' NAME='one' VALUE='a' 'checked' > $cat_answer_b <INPUT TYPE= 'radio' NAME=\"one\" VALUE=\"b\" > $cat_answer_c <INPUT TYPE= 'radio' NAME=\"one\" VALUE=\"c\" > <td> </tr>"; } $display_block .= "</table>"; } ?> Code (markup):
Well, you seem to be missing the <FORM> tags altogether when you build the page? I'm not quite sure what you mean to happen, but, looks to me like you'll list a load of questions with 3 possible answers each. You need to name them differently each time round the loop (rather than calling them "one" each time). So you'd be better off with... <INPUT TYPE='radio' NAME='".$cat_id." VALUE= etc.
Hi nick, Thanks for the advice, but I left the form out because I dont want to display the submit button each time a row is called I only want to show it once for the entire table.
so put the form tag and the submit button outside of the while loop? that way it prints form, then loops through the rows, then prints the button...
here is the code, <?php session_start(); include "db_connect.php"; $get_cats = "select * from tests1"; $get_cats_res = mysql_query($get_cats) or die(mysql_error()); if (mysql_num_rows($get_cats_res) < 1) { $display_block = "<P><em>Sorry, no results.</em></p>"; } else { $display_block .= "<form method = 'post' action ='test_take2.php'> <table width='574' border='1' cellpadding='0' cellspacing='0' > <tr> <td> <h2>$cat_title </h2> </td> </tr>"; $i = 0; while ($cats = mysql_fetch_array($get_cats_res)) { $i++; $cat_id = $cats[id]; $cat_description = stripslashes($cats[description]); $cat_letter = stripslashes($cats[letter]); $cat_answer_a = stripslashes($cats[answer_a]); $cat_answer_b = stripslashes($cats[answer_b]); $cat_answer_c = stripslashes($cats[answer_c]); $display_block .= " <tr> <td width ='30' class='style17'>$cat_id <td/> <td align='center' width ='294' bgcolor='white' class='style19'>$cat_description<td/> <td width ='80'> $cat_answer_a <INPUT TYPE='text' NAME='one[$i]' VALUE='a' 'checked' > $cat_answer_b $cat_answer_c </td> </tr> "; $display_block .= " "; } $display_block .= "<P><input type=\"submit\" name=\"submit\" value=\"Add to Cart\"></p></table> </form>"; } ?>
try something like this. <?php session_start(); include "db_connect.php"; $get_cats = "select * from tests1"; $get_cats_res = mysql_query($get_cats) or die(mysql_error()); if (mysql_num_rows($get_cats_res) < 1) { $display_block = "<P><em>Sorry, no results.</em></p>"; } else { $display_block .= "<form method = 'post' action ='test_take2.php'> <table width='574' border='1' cellpadding='0' cellspacing='0' >". ."<tr><td><h2>".$cat_title."</h2></td></tr>"; $display_block .= '<form action="foo.php" method="post">'; // added this $i = 0; while ($cats = mysql_fetch_array($get_cats_res)) { $i++; $cat_id = $cats[id]; $cat_description = stripslashes($cats[description]); $cat_letter = stripslashes($cats[letter]); $cat_answer_a = stripslashes($cats[answer_a]); $cat_answer_b = stripslashes($cats[answer_b]); $cat_answer_c = stripslashes($cats[answer_c]); $display_block .= " <tr> <td width ='30' class='style17'>".$cat_id." <td/> <td align='center' width ='294' bgcolor='white' class='style19'>".$cat_description."<td/>". "<td width ='80'>".$cat_answer_a."<INPUT TYPE='text' NAME='one[".$i."]' VALUE='a' 'checked' >".$cat_answer_b." ".$cat_answer_c."</td></tr> "; $display_block .= ""; } $display_block .= "<P><input type=\"submit\" name=\"submit\" value=\"Add to Cart\"></p></table> </form>"; } ?> PHP: also, check your table. you're printing the rows in tr's and the error message and submit button in p's. shouldn't they be in a tr/td as well?