tabatsoy Posted March 13, 2008 Share Posted March 13, 2008 Good Day Masters! Please help me with my code. when i'm refreshing my browser everything is ok but when i close it and open it all is gone except submit button. please help me here is my code. i'm using wamp5 <? $db = mysql_connect("localhost"); mysql_select_db("examination",$db); if (isset($_POST['Submit'])) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>dummy</title> </head> <body> <? echo '<FORM name ="form1" method ="post" action ="dummy.php"> <table width="800" border="0" align="center" class="underresult">'; $query = "SELECT * FROM logicQuestions"; $result = mysql_query($query); for($i = 0; $i < mysql_num_rows($result); $i++){ $question_number_logic ++; $question = mysql_result($result, $i, "question"); $choiceA = mysql_result($result, $i, "choiceA"); $choiceB = mysql_result($result, $i, "choiceB"); $choiceC = mysql_result($result, $i, "choiceC"); $choiceD = mysql_result($result, $i, "choiceD"); $answer = mysql_result($result, $i, "answer"); echo '<tr> <td></td>'; '</tr>'; echo '<tr> <td> </td>'; '</tr>'; echo '<tr> <td width = "20" align = "left" valign = "top"><font class = "questions">'.$question_number_logic.'.</span><td>'; echo '<td width = "280" align = "left" valign = "top" ><font class = "questions">'.$question.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125"align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td></tr>'; echo '<tr> <td> </td>'; '</tr>'; echo '<tr> <td width = "20" align = "left" valign = "top"><td>'; echo '<td width = "280" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceA.'" />'.$choiceA.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceB.'" />'.$choiceB.'</span></td>'; echo '<td width = "125"align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceC.'" />'.$choiceC.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceD.'" />'.$choiceD.'</span></td></tr>'; echo '<tr> <td colspan = "7" class = "exam">'.$selected = $_POST[$question_number_logic]; if ($selected == $answer) { $score++; } }' </td> </tr>'; } echo '</table> <br /> total score is : '.$score.'<br /> <Input type = "Submit" Name = "Submit" VALUE = "Select a Radio Button"> </FORM>'; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/ Share on other sites More sharing options...
revraz Posted March 13, 2008 Share Posted March 13, 2008 Do some error checking since your page relies on you pulling data from the DB $result = mysql_query($query) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491380 Share on other sites More sharing options...
soycharliente Posted March 13, 2008 Share Posted March 13, 2008 Is it just me or is your entire page wrapped in an if. Maybe that's why there's nothing displaying? Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491384 Share on other sites More sharing options...
sasa Posted March 13, 2008 Share Posted March 13, 2008 when you go direct to this page $_POST isn't set yet and page don't show part from if (isset($_POST['Submit'])) { to last one } Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491387 Share on other sites More sharing options...
tabatsoy Posted March 13, 2008 Author Share Posted March 13, 2008 yes my whole page is wrapped in if statement. what should i do please help Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491391 Share on other sites More sharing options...
conker87 Posted March 13, 2008 Share Posted March 13, 2008 Get rid of it ^^ Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491394 Share on other sites More sharing options...
tabatsoy Posted March 13, 2008 Author Share Posted March 13, 2008 oh no!!! you mean to get rid of the whole page? help me please Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491397 Share on other sites More sharing options...
soycharliente Posted March 13, 2008 Share Posted March 13, 2008 You have a form. It is inside an if. The if checks to see if the form is submitted. How could you see the form to submit it if you can't submit it in order to see it? LOL. I'm confusing myself. The problem isn't that the form is inside the if (though it may be), it's that you don't have an else to display something different when the form hasn't been submitted. What is the page supposed to be doing? Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491402 Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 The code is sloppy. Please tabulate properly and wrap in [ code ] [ /code ] tags if you need help that bad. I really don't see the logic at first glance, but that's probably because I have no idea when the if statements begin or end. Why do you have the form declaration within the if (submit) clause? Why do you use echo for some html, but end php parsing for others? Throw some logic at your code and it might start working. Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491403 Share on other sites More sharing options...
tabatsoy Posted March 13, 2008 Author Share Posted March 13, 2008 here is my new code. sorry for the sloppy one. <? $db = mysql_connect("localhost"); mysql_select_db("examination",$db); if (isset($_POST['Submit'])) { echo '<FORM name ="form1" method ="post" action ="dummy.php"> <table width="800" border="0" align="center" class="underresult">'; $query = "SELECT * FROM logicQuestions"; $result = mysql_query($query); for($i = 0; $i < mysql_num_rows($result); $i++){ $question_number_logic ++; $question = mysql_result($result, $i, "question"); $choiceA = mysql_result($result, $i, "choiceA"); $choiceB = mysql_result($result, $i, "choiceB"); $choiceC = mysql_result($result, $i, "choiceC"); $choiceD = mysql_result($result, $i, "choiceD"); $answer = mysql_result($result, $i, "answer"); echo '<tr> <td></td>'; '</tr>'; echo '<tr> <td> </td>'; '</tr>'; echo '<tr> <td width = "20" align = "left" valign = "top"><font class = "questions">'.$question_number_logic.'.</span><td>'; echo '<td width = "280" align = "left" valign = "top" ><font class = "questions">'.$question.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125"align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td></tr>'; echo '<tr> <td> </td>'; '</tr>'; echo '<tr> <td width = "20" align = "left" valign = "top"><td>'; echo '<td width = "280" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceA.'" />'.$choiceA.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceB.'" />'.$choiceB.'</span></td>'; echo '<td width = "125"align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceC.'" />'.$choiceC.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceD.'" />'.$choiceD.'</span></td></tr>'; echo '<tr> <td colspan = "7" class = "exam">'.$selected = $_POST[$question_number_logic]; if ($selected == $answer) { $score++; } }' </td> </tr>'; } echo '</table> <br /> total score is : '.$score.'<br /> <Input type = "Submit" Name = "Submit" VALUE = "Select a Radio Button"> </FORM>'; ?> please help Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491412 Share on other sites More sharing options...
conker87 Posted March 13, 2008 Share Posted March 13, 2008 Argh! Just take out the if statement and it's corresponding curly brackets then try. Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491417 Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 The problem is quite obvious... if a submit button hasn't been pressed, lines 7 through 55 will not be executed. Try removing it along with the closing curly brace on line 56. I'm really not sure why it's there. I don't see any insertion queries. Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491420 Share on other sites More sharing options...
tabatsoy Posted March 13, 2008 Author Share Posted March 13, 2008 arg!!! removing the if statements and the curly braces did'nt work Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491429 Share on other sites More sharing options...
tabatsoy Posted March 13, 2008 Author Share Posted March 13, 2008 thank you masters for the help please check if i did it right. i think it is because it's working now <? $db = mysql_connect("localhost"); mysql_select_db("examination",$db); if(isset($_POST['Submit'])){ echo '<FORM name ="form1" method ="post" action ="dummy.php"> <table width="800" border="0" align="center" class="underresult">'; $query = "SELECT * FROM logicQuestions"; $result = mysql_query($query); for($i = 0; $i < mysql_num_rows($result); $i++){ $question_number_logic ++; $question = mysql_result($result, $i, "question"); $choiceA = mysql_result($result, $i, "choiceA"); $choiceB = mysql_result($result, $i, "choiceB"); $choiceC = mysql_result($result, $i, "choiceC"); $choiceD = mysql_result($result, $i, "choiceD"); $answer = mysql_result($result, $i, "answer"); echo '<tr> <td></td>'; '</tr>'; echo '<tr> <td> </td>'; '</tr>'; echo '<tr> <td width = "20" align = "left" valign = "top"><font class = "questions">'.$question_number_logic.'.</span><td>'; echo '<td width = "280" align = "left" valign = "top" ><font class = "questions">'.$question.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125"align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td></tr>'; echo '<tr> <td> </td>'; '</tr>'; echo '<tr> <td width = "20" align = "left" valign = "top"><td>'; echo '<td width = "280" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceA.'" />'.$choiceA.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceB.'" />'.$choiceB.'</span></td>'; echo '<td width = "125"align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceC.'" />'.$choiceC.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceD.'" />'.$choiceD.'</span></td></tr>'; echo '<tr> <td colspan = "7" class = "exam">'.$selected = $_POST[$question_number_logic]; if ($selected == $answer) { $score++; } }' </td> </tr>'; } else{ echo '<FORM name ="form1" method ="post" action ="dummy.php"> <table width="800" border="0" align="center" class="underresult">'; $query = "SELECT * FROM logicQuestions"; $result = mysql_query($query); for($i = 0; $i < mysql_num_rows($result); $i++){ $question_number_logic ++; $question = mysql_result($result, $i, "question"); $choiceA = mysql_result($result, $i, "choiceA"); $choiceB = mysql_result($result, $i, "choiceB"); $choiceC = mysql_result($result, $i, "choiceC"); $choiceD = mysql_result($result, $i, "choiceD"); $answer = mysql_result($result, $i, "answer"); echo '<tr> <td></td>'; '</tr>'; echo '<tr> <td> </td>'; '</tr>'; echo '<tr> <td width = "20" align = "left" valign = "top"><font class = "questions">'.$question_number_logic.'.</span><td>'; echo '<td width = "280" align = "left" valign = "top" ><font class = "questions">'.$question.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125"align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td></tr>'; echo '<tr> <td> </td>'; '</tr>'; echo '<tr> <td width = "20" align = "left" valign = "top"><td>'; echo '<td width = "280" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceA.'" />'.$choiceA.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceB.'" />'.$choiceB.'</span></td>'; echo '<td width = "125"align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceC.'" />'.$choiceC.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" ><font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceD.'" />'.$choiceD.'</span></td></tr>'; echo '<tr> <td colspan = "7" class = "exam">'.$selected = $_POST[$question_number_logic]; if ($selected == $answer) { $score++; } }' </td> </tr>'; } echo '</table> <br /> total score is : '.$score.'<br /> <Input type = "Submit" Name = "Submit" VALUE = "Select a Radio Button"> </FORM>'; ?> one last. how can i shorten this code? Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491434 Share on other sites More sharing options...
trq Posted March 13, 2008 Share Posted March 13, 2008 Notice how every other piece of code in our forum is syntax highlighted? Please use tags when posting code! Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491440 Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 After looking at the code i really can't believe PHP actually parsed it Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491443 Share on other sites More sharing options...
soycharliente Posted March 13, 2008 Share Posted March 13, 2008 After look at it as well, it does have a FEW errors. None that warrant "i really can't believe PHP actually parsed it" since tab does have 6 posts and may need some help. They seem to be the same error just repeated. Looks like you just forgot to put echo at the beginning of the line. Also, you tried to set a variable equal to something at the exact same time as using it. You either need to just do the $_POST value or save it before you reference it. Here's your code, tabbed and commented and with , so you know what is wrong: <?php $db = mysql_connect("localhost"); mysql_select_db("examination",$db); if(isset($_POST['Submit'])){ echo '<FORM name ="form1" method ="post" action ="dummy.php"> <table width="800" border="0" align="center" class="underresult">'; $query = "SELECT * FROM logicQuestions"; $result = mysql_query($query); for($i = 0; $i < mysql_num_rows($result); $i++) { $question_number_logic++; $question = mysql_result($result, $i, "question"); $choiceA = mysql_result($result, $i, "choiceA"); $choiceB = mysql_result($result, $i, "choiceB"); $choiceC = mysql_result($result, $i, "choiceC"); $choiceD = mysql_result($result, $i, "choiceD"); $answer = mysql_result($result, $i, "answer"); echo '<tr><td></td>'; '</tr>'; // what is this? echo '<tr><td> </td>'; '</tr>'; // what is this? echo '<tr> <td width = "20" align = "left" valign = "top"> <font class = "questions">'.$question_number_logic.'.</span><td>'; echo '<td width = "280" align = "left" valign = "top" > <font class = "questions">'.$question.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125"align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td></tr>'; echo '<tr><td> </td>'; '</tr>'; // what is this? echo '<tr><td width = "20" align = "left" valign = "top"><td>'; echo '<td width = "280" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > <font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceA.'" />'.$choiceA.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > <font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceB.'" />'.$choiceB.'</span></td>'; echo '<td width = "125"align = "left" valign = "center" > <font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceC.'" />'.$choiceC.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > <font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceD.'" />'.$choiceD.'</span></td></tr>'; echo '<tr><td colspan = "7" class = "exam">'.$selected = $_POST[$question_number_logic]; // what is this? if ($selected == $answer) { $score++; } } ' </td></tr>'; // what is this? } else { echo '<FORM name ="form1" method ="post" action ="dummy.php"> <table width="800" border="0" align="center" class="underresult">'; $query = "SELECT * FROM logicQuestions"; $result = mysql_query($query); for($i = 0; $i < mysql_num_rows($result); $i++) { $question_number_logic++; $question = mysql_result($result, $i, "question"); $choiceA = mysql_result($result, $i, "choiceA"); $choiceB = mysql_result($result, $i, "choiceB"); $choiceC = mysql_result($result, $i, "choiceC"); $choiceD = mysql_result($result, $i, "choiceD"); $answer = mysql_result($result, $i, "answer"); echo '<tr><td></td>'; '</tr>'; // what is this? echo '<tr><td> </td>'; '</tr>'; // what is this? echo '<tr><td width = "20" align = "left" valign = "top"> <font class = "questions">'.$question_number_logic.'.</span><td>'; echo '<td width = "280" align = "left" valign = "top" > <font class = "questions">'.$question.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td>'; echo '<td width = "125"align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > </td></tr>'; echo '<tr><td> </td>'; '</tr>'; // what is this? echo '<tr><td width = "20" align = "left" valign = "top"><td>'; echo '<td width = "280" align = "left" valign = "center" > </td>'; echo '<td width = "125" align = "left" valign = "center" > <font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceA.'" />'.$choiceA.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > <font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceB.'" />'.$choiceB.'</span></td>'; echo '<td width = "125"align = "left" valign = "center" > <font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceC.'" />'.$choiceC.'</span></td>'; echo '<td width = "125" align = "left" valign = "center" > <font class = "style1"><input name="'.$question_number_logic.'" type="radio" value="'.$choiceD.'" />'.$choiceD.'</span></td></tr>'; echo '<tr><td colspan = "7" class = "exam">'.$selected = $_POST[$question_number_logic]; // what is this? if ($selected == $answer) { $score++; } } ' </td></tr>'; // what is this? } echo '</table> total score is : '.$score.' <Input type = "Submit" Name = "Submit" VALUE = "Select a Radio Button"> </FORM>'; ?> Also, is it just me or is there absolutely no difference between what's inside the if and what's inside the else? Why even have an if then? Just paste the code if that's the case. 700 posts!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/#findComment-491491 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.