John Greg John Greg - 8 days ago 7
Javascript Question

Read input and echo this else that with php

I'm working on a form.php file which is structured below:

<form action="action.php" method="post">
<input name="answer" type="text">
<input name="submit" type="submit" value="Submit">
<input name="reset" type="reset" value="Reset">
</form>


...and I want to add some php or javascript code which will read the value of the input field that the user will type and if the value is equal to a then echo true else echo false. Until now, I made some attempts like the one below but don't work.

<?php
$_POST($answer);

if ($answer == "a") {
echo "True";
} else {
echo "False";
}
?>


Maybe is not that simple as I think. Any ideas or better suggestions?

Answer

Your code was quite correct I've just added a few improvements

 <?php
    $answer = isset($_POST['answer']) ? $_POST['answer'] : null;
    if ($answer == "a") {
        echo "True";
    } else if (!is_null($answer)) {
        echo "False";
    }
 ?>