Good day. I can not understand how to correctly check the form in php.

<!-- Область основного контента --> <form action='calc.php' method="get"> <label>Число 1:</label> <br /> <input name='num1' type='text' /> <br /> <label>Оператор: </label> <br /> <input name='operator' type='text' /> <br /> <label>Число 2: </label> <br /> <input name='num2' type='text' /> <br /> <br /> <input type='submit' value='Считать'> </form> <!-- Область основного контента --> <?php // var_dump($_GET); if (empty($_GET)){ return 'Ничего не передано!'; } if (empty($_GET['num1'])|| empty($_GET['num2'])) { return 'Не переданы аргументы!'; } if (empty($_GET['operator'])) { return 'Не передана операция'; } ?> 

Interested in such questions: 1) Can I return the result via return or write via echo? 2) When I wrote through echo, the second and third checks worked, but 1 never worked ...

  • You can use returt if you write verification functions and use echo already when executing it. Or when using ajax. And so I would advise using method="post" because get zahlomit you search bar - Ruslan Semenov

1 answer 1

1) echo as the PHP script is executed on the server side, and the user sending the request sees the result of this script

2) Well, since you sent GET through a form and a GET array, it looked like this: (Array (2) => "name" => "", "num2" => "")

  • why don’t work the if (empty ($ _ GET)) lines {echo 'Nothing transmitted!'; } ??? - White_Snow
  • @White_Snow write var_dump ($ _ GET) at the beginning of the script and discard the result if you do not understand what the joke is :) - BigTows
  • array (3) {["num1"] => string (0) "" ["operator"] => string (0) "" ["num2"] => string (0) ""} Well, the array is empty - White_Snow
  • one
    @White_Snow, no :) Under the elements, the arrays are empty, and the Array itself has the values ​​num1 operator num2, as many as 3 !!! so it's not empty - BigTows
  • @White_Snow, by the way, var_dump brought you the dimension of the array array (3) I 3 - BigTows