There's one small issue with your current code: You're using =
to compare a value to another, which will not work, single =
is used to assign a variable. You should use ==
instead.
When you want to use a user's input, you will need something called $_POST
, this is a method that you can set in a form using the method="POST"
attribute. When the form is submitted it will create an array with the values in the form.
You can then access these values using a certain key, which is equal to the name=""
attribute of the input, select or textarea in the form.
Example
Consider this form, with some PHP code:
<form method="post">
<input type="text" name="myName" placeholder="Enter your name!">
<input type="submit" value="Submit">
</form>
<?php
// When the server gets a $_POST request
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Set the variable name to whatever the user put in
$name = $_POST['myName']; // equal to the name="" attribute in the form
echo "Hello, " . $name . "!";
}
?>
If I submit the form with my name, it will echo: Hello, rpm192!
For your situation, it would look something like this:
<form method="post">
<input type="number" name="answer" placeholder="Your answer">
<input type="submit" value="Submit answer">
</form>
<?php
// When the server gets a $_POST request
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Set the variable X to whatever the user put in
$x = $_POST['answer'];
$answer = ($x = 1);
// Check if $answer is true or false
if($answer) {
echo "True!";
} else {
echo "False!";
}
}
?>