Hi,
This is my first post here. I'm pretty new with PHP. I've been trying to learn it on my own with the help of some books.
I tried to use what I've learned so far to make a quiz with php, just to see if I could do it. The first page would use HTML forms to ask the questions, and clicking the submit button takes you to a php page, which gives you your score, and tells you which answers were correct.
When I tried out the quiz, it seemed to work okay, when I entered all correct answers. However, when I took the quiz again, entering incorrect answers, it still gave me a perfect score. Here is the code for the first page, which you can find at http://www.jmussehl.com/quiz.html
<form action="answers.php" method="post">
<p>1. What is the capital of New York? <input type="text" name="1" /></p>
<p>2. What is the capital of Nebraska? <input type="text" name="2" /></p>
<p>3. What is the capital of Texas? <input type="text" name="3" /></p>
<p>4. What is the capital of California? <input type="text" name="4" /></p>
<p>5. What is the capital of Pennsylvania? <input type="text" name="5" /></p>
<p><input type="submit" value="check my answers" />
Here is the code for the second page, which you can find at: http://www.jmussehl.com/answers.php
<?php
$score = 0;
$incorrect = "<span style='color:red; font-weight:bold;'>Incorrect.</span>";
$correct = "<b>Correct!</b>";
if ($_POST['1'] = "Albany" )
{
$message1 = $correct;
$score++;
}
else {
$message1 = $incorrect;
}
if ($_POST['2'] = "Lincoln" )
{
$message2 = $correct;
$score++;
}
else {
$message2 = $incorrect;
}
if ($_POST['3'] = "Austin" )
{
$message3 = $correct;
$score++;
}
else {
$message3 = $incorrect;
}
if ($_POST['4'] = "Sacramento" )
{
$message4 = $correct;
$score++;
}
else {
$message4 = $incorrect;
}
if ($_POST['5'] = "Harrisburg" )
{
$message5 = $correct;
$score++;
}
else {
$message5 = $incorrect;
}
?>
<p>You got <?php print $score; ?> out of 5 questions correct</p>
<p>1. Your answer: <?php print $_POST['1'].". ".$message1; ?></p>
<p>2. Your answer: <?php print $_POST['2'].". ".$message2; ?></p>
<p>3. Your answer: <?php print $_POST['3'].". ".$message3; ?></p>
<p>4. Your answer: <?php print $_POST['4'].". ".$message4; ?></p>
<p>5. Your answer: <?php print $_POST['5'].". ".$message5; ?></p>
Can anyone advise me on what I'm doing wrong?