0

I am trying to get the user input and verify if the answer is correct using an if and statement. How do I prompt the user for input within the same webpage? This is the code I have so far.

<?php
$answer = //user input
if ($answer = "4") {
  echo " “Correct!!!! Congratulations!";
} else {
  echo "Your answer was blank but the correct answer is 4.";
}
?>
  • 1
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – B001ᛦ May 26 '21 at 12:25
  • 1
    If this is PHP for a web page (not executed on the command line) then you don't use PHP to "prompt" the user. Any user interface/interaction is in HTML/CSS/JavaScript on the web page. It sounds like what you're really looking for are some introductory tutorials on PHP. (As an aside... `if ($answer = "4")` will *always* be true.) – David May 26 '21 at 12:25
  • In php you can use forms sending POST/GET requests to the server. BTW `if ($answer = "4")` shoul dchange to `if ($answer == "4")` – B001ᛦ May 26 '21 at 12:26
  • 1
    This is the kind of thing you can learn from beginner tutorials already. The answer below gives you a nice sample but really it should not have been needed, you can find this kind of thing in 100 places already. – ADyson May 26 '21 at 12:30
  • If you just want to run it in a command line interface, use `fscanf(STDIN, '%d\n', $answer);` – shingo May 26 '21 at 14:33

1 Answers1

1

This is the full form view to let you know how to perform the basic form submit and get values and displaying the result

<?php
if(isset($_POST['submit']))
{
    $answer=$_POST['answer'];
    if ($answer == "4") {
  echo " “Correct!!!! Congratulations!";
} else {
  echo "Your answer was blank or wrong input but the correct answer is 4.";
}
}
?>
<html>
<body>
<form method="post" action="index.php">
<input type="number" name="answer">
<input type="submit" name="submit" value="Submit">
</form>
Muhammad Asif
  • 456
  • 4
  • 10