Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // variables to represent different message generated depending on user input submission
- $validation_error = ""; //the inner HTML of our error <span>.
- $user_answer = ""; // assigned to the value attribute of the "answer" input element
- $submission_response = "";
- // Write Function here
- if ($_SERVER["REQUEST_METHOD"] === "POST") {
- $user_answer = filter_var($_POST["answer"], FILTER_SANITIZE_NUMBER_INT); // inbuilt sanitization filter to sanitize integers. First argument - variable, second argument - takes an ID representing the type of filtering that should be performed.
- if ($user_answer != "-5"){
- $validation_error = "* Wrong answer. Try again.";
- } else {
- $submission_response = "Correct!";
- }
- }
- ?>
- <h2>Time for a math quiz!</h2>
- <form method="post" action="">
- <h4>Question 1:</h4>
- <p>What is 6 - 11?</p>
- <input type="text" name="answer" id="answer" value="<?= $user_answer;?>">
- <br>
- <span class="error" id="error"><?= $validation_error;?></span> <!--if wrong, message to print will be here -->
- <br>
- <input type="submit" value="Submit Your Answer">
- </form>
- <div>
- <p id="answer-display">Your answer was: <?= $user_answer;?></p>
- <p id="submission-response"><?= $submission_response;?></p> <!-- print message when it's correct answer-->
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement