0

The echo statement is not printing when the answer does not equal to 8.So if the answer is other than 8 it should print in red "Please try again " underneath the textbox.All it shows is nothing when I click the register button.

   <?php

   session_start();
    $nameErr = "";


   $answer = $_POST['lastname'];

   if ($_POST['reg']){


     if ($answer != '8') {
        echo "<span style='color:red'>Please enter Correct Answer</span>";
     }
     }
    ?>
   <!Doctype html>
   <head>
   <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" 
   type="text/css">
   <link href="bootstrap/css/styles.css" rel="stylesheet" type="text/css">
   <link rel="stylesheet" type="text/css" href="Register.css">

   <script src='https://www.google.com/recaptcha/api.js'></script>

       <title> Register</title>



        </head>

        <html>
        <body>
        <form method="post" action="">
        What is 5 + 3 =<br>
        <input type="text" name="lastname"> 





       <div class="row">
      <div class="form-group col-xs-5 col-xs-offset-3.5">
     <input class="btn btn-primary btn-lg btn-block"  value="Register" 
     type="submit" name="reg">

2

2 Answers 2

1

You need to put the echo in the <body> of the document. Also, <html> needs to be at the top.

<?php

session_start();
$nameErr = "";

?>
<!Doctype html>
<html>

<head>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" 
    type="text/css">
    <link href="bootstrap/css/styles.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="Register.css">
    <script src='https://www.google.com/recaptcha/api.js'></script>
    <title> Register</title>
</head>

<body>
    <form method="post" action="">
    What is 5 + 3 =<br>
    <input type="text" name="lastname"> 
    <?php
    if (isset($_POST['reg'])){
        $answer = $_POST['lastname'];
        if ($answer != '8') {
            echo "<span style='color:red'>Please enter Correct Answer</span>";
        }
    }
    ?>
    <div class="row">
        <div class="form-group col-xs-5 col-xs-offset-3.5">
            <input class="btn btn-primary btn-lg btn-block"  value="Register" type="submit" name="reg">
        </div>
    </div>
    </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

11 Comments

I tried that by putting the html first and then the php code at the end but the message appears please enter correct answer even if I dont enter nothing in the textbox
If you enter nothing in the textbox, that's not the same as 8, so you get the error message.
Nope the error message does not have the red colour so it should only come when I enter a value into the textbox click register and if it is not 8 then it should show the error message in red
Do you see the span if you use View Source? Use the DOM inspector to see what's overriding the style.
Maybe something in Twitter Bootstrap is doing it.
|
0

You print the error span above the actual HTML code. Just move the if statement down. Like that:

...
<div class="row">
<?php
if ($answer != '8') {
    echo "<span style='color:red'>Please enter Correct Answer</span>";
}
?>
</div>
<div class="form-group col-xs-5 col-xs-offset-3.5">
...

1 Comment

@MajedMahmood - yes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.