Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/95982-help-me-with-my-codeurgent/
Share on other sites

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?

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.

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

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.

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?

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!!!!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.