what is wrong with the code below?
Question page
<html>
<head><title>ex41a</title></head>
<body>
<form method="post" name="ex41a" action="ex41b.php">
<hr>
Q1: Who made this question? <br>
<input type="radio" name="1ans" value="Spiderman"> Spiderman <br>
<input type="radio" name="1ans" value="Ed"> Ed <br>
<input type="radio" name="1ans" value="Superman"> Superman <br>
<input type="radio" name="1ans" value="The Hulk"> The Hulk <br>
<hr>
Q2: What is this for? <br>
<input type="radio" name="2ans" value="Exercise"> Exercise <br>
<input type="radio" name="2ans" value="Your own self"> Your own self <br>
<input type="radio" name="2ans" value="Practice"> Practice <br>
<input type="radio" name="2ans" value="Nothing"> Nothing <br>
<hr>
Q3: Who开发者_JAVA百科 is the teacher? <br>
<input type="radio" name="3ans" value="Mr. Lo"> Mr. Lo <br>
<input type="radio" name="3ans" value="Mr. Lai"> Mr. Lai <br>
<input type="radio" name="3ans" value="Mr. Ivan"> Mr. Ivan <br>
<input type="radio" name="3ans" value="Mr. Chow"> Mr. Chow <br>
<hr>
<input type="submit" name="Submit">
</body>
</html>
point page
<html>
<head><title>ex41b</title></head>
<body bgcolor="silver" text="black">
<body>
<font size=30 color=blue>Your total point is:</font><hr>
<?php
if($_POST["1ans"]=="Ed") {
$result1=1;
}else{
$result1=0;
}
if($_POST["2ans"]=="Practice") {
$result2=1;
}else{
$result2=0;
}
if($_POST["3ans"]=="Mr Chow") {
$result3=1;
}else{
$result3=0;
}
echo $_POST["result1"]+$_POST["result2"]+$_POST["result3"];
?>
<hr>
<a href="ex41a.php">Back</a>
</body>
</html>
I tried this code already, but if I choose the right answer I still get 0 point.... how can I make it like if there's a right answer then it will add 1 point in total of 3 points...
Q1 ans is Ed
Q2 ans is Practice
Q3 ans is Mr Chow
Thank you!!!
Your code is very ugly, but to solve this problem start by using the variables you have introduced instead of undefined POST variables:
echo $result1+$result2+$result3;
And here's a suggestion for a better structure:
$correct = array(1 => 'Ed',
2 => 'Practice',
3 => 'Mr Chow');
$result = array();
$points = 0;
for($i = 1; $i <= 3; $i++) {
if($_POST['ans'.$i] == $correct[$i]) {
$result[$i] = true;
$points++;
}else{
$result[$i] = false;
}
}
echo $points;
The $result
will now contain if the user answered correctly on each question, and $points
is the total number of correct answers. Notice that I prefer arrays instead of numbered variable names since it's much easier to work with arrays.
use
echo $result1 + $result2 + $result3;
instead of
echo $_POST["result1"]+$_POST["result2"]+$_POST["result3"];
Change this line:
echo $_POST["result1"]+$_POST["result2"]+$_POST["result3"];
To:
echo $result1+$result2+$result3;
Consider this: How are the values from your variables supposed to end up in the $_POST
superglobal?
you need to: echo $result1+$result2+$result3;
rather than the post values
精彩评论