Good day! Tell me, please, the answer.

There is an input form. The user enters the number in the form. If the number coincided, so that in the database, then we give out: the number is correct. Otherwise, the number is not correct. Question: how to make a warning: "the number is not entered in the field", if the user has not entered a number, and pressed the button "next". Code below.

<? if (empty($_POST['chislo'])) { echo '<form action="?" method="post"><div> Число:<br/><input type="text" name="chislo" maxlength="40" title="Введите число"><br> <input type="submit" value="Проверка" title="Проверка"/></div></form>'; } else{ $chi = intval($_POST['chislo']); //фильтр } else { $chislo = mysql_fetch_assoc(mysql_query("SELECT `ch` FROM `chislo`")); // база. if ($chi == $chislo['ch']) { echo 'число правильное'; } else { echo 'число не правильное'; } } ?> 
  • What is this terrible nightmare? From where are two else in a row? A check that the number is not entered when you press the button, you need to do in JavaScript - Photon
  • Photon, I need a php check. - Borisov_
  • for html5 use the required attribute (the field must be filled in). On php normal check. And check for javascript on the number. Or do you need help with the implementation? - forum3

5 answers 5

Well, if in PHP, then so:

 if (!isset($_POST['test'])) { ?> <form action="" method="post"> <div> Число:<br/><input type="text" name="chislo" maxlength="40" title="Введите число"><br> <input type="submit" name="test" value="Проверка" title="Проверка"/> </div> </form> <?php } elseif (isset($_POST['test']) && trim($_POST['chislo']) == "") { echo "Не введено число"; } else{ $chi = intval($_POST['chislo']); //фильтр $chislo=mysql_fetch_assoc(mysql_query("SELECT `ch` FROM `chislo`")); // база. if($chi==$chislo['ch']){ echo'число правильное'; } else { echo 'число не правильное'; } } 

    Do you need a pop-up window or what?

    Where you are

    echo 'the number is not correct';

     echo "<script>alert('Введите число правильно!')</script>"; 

      else echo 'Число неправильное' well, just the person did not put a semicolon.

       else echo 'Число неправильное'; 

        Something like this:

         <?php $number = $_POST['chislo']; $number_from_bd = 123; // число из базы if($_POST['submit']) { if(isset($number)) { if($number == $number_from_bd) echo 'Число правильное'; else echo 'Число неправильное'; } else echo 'Введите число'; } ?> <form action="?" method="post"> Число: <br/> <input type="text" name="chislo" maxlength="40" title="Введите число"> <br> <input name="submit" type="submit" value="Проверка" title="Проверка"/> </form> 
        • Parse error: syntax error, unexpected '}', expecting ',' or ';' on line 24 <? } else echo 'Enter a number'; ?> - Borisov_
        • Cool, and I’m an evil hacker and bahnu in the '1' field or 1 space, and what are we going to do? - Gena Ant
        • @Borisov_, edited a post. - ModaL
        • @Genchik, did not think. But here you can do with the trim () function. It is about spaces. And about checking for the input of the number, there was no talk. - ModaL

        So, too, you can + add a filter to php and everything will be good:

         <input type="text" name="chislo" maxlength="40" title="Введите число" required>