so for a class assignment we are using HTML and PHP to make a form that asks a user to input two (2) numbers and to choose between addition, subtraction, multiplication or division form using radio buttons, THEN to input a guess as to what the answer is. Our assignment is to echo out to the user specifying whether or not their answer is correct or not.
The part where I am having troubles is in the "if" and "else" statements. For example, if I choose 1 and 2 as my numbers, guess 3 and choose addition as my method of calculation, I not only get echoed out "Congratulations! You choose the correct answer of 3!" but i also get my else statements from division, subtraction, and multiplication specifying that I did it incorrectly.
Here's my Code:(keep in mind-- the birthday function has yet to be made.) Also, I'm having difficulties regarding my form validation -- making it so that the user has in fact entered the required fields.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Howdy</title>
</head>
<body>
<h1>PHP_SELF</h1>
<?php
//process the form if the submit button was pressed
if (isset($_POST['submit'])) {
//form validation goes here
/////////////////////////////////////////VARIABLES ARE BEING MADE HERE
//simplify the form variables
//later on we will do this in form validation
//create a variable called firstname and store in it
//the value from the POST array for firstname from the form
$firstname = $_POST['firstname'];
//creating variables for num1 and num2 that user inputed
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
//creating a variable called sum
//this is for when the user chooses to ADD
//num1 and num2
$sum = $num1 + $num2;
//creating a variable called difference
//this is for when the user chooses to SUBTRACT
//num1 and num2
$difference = $num1 - $num2;
//creating a variable called product
//this is for when the user chooses to MULTIPLY
//num1 and num2
$product = $num1 * $num2;
//creating a variable called quotient
//this is for when the user chooses to DIVIDE
//num1 and num2
$quotient = $num1 / $num2;
//creating a variable called guess and store it in the
//value from the POST array for the guess from the form
$guess = $_POST['guess'];
//creating a variable called birthday and store it in the
//value from the POST array for the user's birthday
$birthday = $_POST['birthday'];
if ($sum == $guess) {
echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";
} else if ($sum != $guess){
echo "<p>$firstname, you answered incorrectly. The correct answer is $sum.</p>\n";
}
if ($difference == $guess) {
echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";
} else if ($difference != $guess){
echo "<p>$firstname, you answered incorrectly. The correct answer is $difference.</p>\n";
}
if ($product == $guess) {
echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";
} else if ($product != $guess){
echo "<p>$firstname, you answered incorrectly. The correct answer is $product.</p>\n";
}
if ($quotient == $guess) {
echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";
} else if ($quotient != $guess){
echo "<p>$firstname, you answered incorrectly. The correct answer is $quotient.</p>\n";
}
} //end of the isset submit conditional statement
//show the form if it is the user's first time her OR if any of the required forms are missing
if(!isset($_POST ['submit']) OR empty($firstname) OR empty($num1) OR empty($num2)) { ?>
<h2>Please fill out the following: </h2>
<!--FORM BEGINS-->
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><label for="firstname">Please enter your first name: </label>
<input id="firstname" type="text" size="30" name="firstname" value="<?php if(isset($firstname)) echo $firstname; ?>"/><p>
<!--Challenge Dealio-->
<p><label for="Num1">Please enter a number: </label>
<input id="Num1" type="number" size="30" name="num1" value="<?php if(isset($num1)) echo $num1; ?>" /></p>
<p><label for="Num2">Please enter another number: </label>
<input id="Num2" type="number" size="30" name="num2" value="<?php if(isset($num2)) echo $num2; ?>"/><p>
<p>Please choose one of the following: </p>
<p>
<input name="add" id="answer" type="radio" value="add" />Add<br />
<input name="subtract" id="answer" type="radio" value="subtract" />Subtract<br />
<input name="multiply" id="answer" type="radio" value="multiply" />Multiply<br />
<input name="divide" id="answer" type="radio" value="divide" />Divide<br />
</p>
<p><label for="guess">Please put in a guess for the answer: </label>
<input id="guess" type="number" size="30" name="guess" value="<?php if(isset($guess)) echo $guess; ?>"/></p>
<p><label for="birthday">Please enter your birth date: </label>
<input id="birthday" type="date" size="30" name="birthday" value="<?php if(isset($birthday)) echo $birthday; ?>"/></p>
<!--Submit Button-->
<input type="submit" name="submit" value="Enter" />
</form>
<?php
} //end form conditional statement
?>
</body>
</html>
What did I do wrong? And how can I go about fixing this?
First of all al radio buttons should have same name with different value. And in same page you cannot have same id's
<p>
<input name="action" id="add" type="radio" value="add" />Add<br />
<input name="action" id="subtract" type="radio" value="subtract" />Subtract<br />
<input name="action" id="multiply" type="radio" value="multiply" />Multiply<br />
<input name="action" id="divide" type="radio" value="divide" />Divide<br />
</p>
Then in your php to perform selected option,
if($_POST['action'] == "add") {
$result = $num1 + $num2;
} else if($_POST['action'] == "subtract") {
$result = $num1 - $num2;
} else if($_POST['action'] == "multiply") {
$result = $num1 * $num2;
} else if($_POST['action'] == "divide") {
$result = $num1 / $num2;
}
Rename all values to $result
.
if($result == $guess) {
echo "<p>Congratulations $firstname. You answered correctly with $guess.<p>\n";
} else {
echo "<p>$firstname, you answered incorrectly. The correct answer is $difference.</p>\n";
}
No need to check so many times, take the result to 1 variable, check it only once.
Edit : For birthday part In View
<select name="year">add options here</select>
<select name="month">add options here</select>
<select name="day">add options here</select>
In PHP,
$month = $_POST['month'];
$year = $_POST['year'];
$day = $_POST['day'];
$date = $year ."-". $month ."-".$day;
$date = date("Y-m-d",strtotime($date));
if(date('m-d') == date('m-d', $date)) {
// today is users birthday. show any message you want here.
}
First of all you cannot have the same ids on your radios. They must be different with the same name.
<p>
<input name="action" id="add" type="radio" value="add" />Add<br />
<input name="action" id="subtract" type="radio" value="subtract" />Subtract<br />
<input name="action" id="multiply" type="radio" value="multiply" />Multiply<br />
<input name="action" id="divide" type="radio" value="divide" />Divide<br />
</p>
Then you are not validating which function the user is choosing via the checkboxes. You are iterating through all the if
and else
without filtering which function was chosen.
What I would do is use a Switch Case statement to filter which method of calculation was chosen and then compare if the guess is right. Something like:
switch $answer {
case "add":
//Check if guess is right and echo
break;
case "substract":
//Check if guess is right and echo
break;
case "multiply":
//Check if guess is right and echo
break;
case "divide":
//Check if guess is right and echo
break;
};
If you are not allowed to use the Switch Case Statement then you should first check which answer was marked and then if the guess is right:
if($answer == "add"){
if($sum == $guess){
echo "Congrats";
} else {
echo "error";
}
} else if($answer == "substract"){
if($difference == $guess){
echo "Congrats";
} else {
echo "error";
}
} else if($answer == "multiply"){
if($product == $guess){
echo "Congrats";
} else {
echo "error";
}
} else if($answer == "divide"){
if($quotient == $guess){
echo "Congrats";
} else {
echo "error";
}
}
Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.