Such a task: checking the entered data on the server side, interrupt the sending script and display the corresponding error message (such as "the entered name is too short", "the phone number is entered incorrectly", etc.).

What I do not understand is how to return any value when the script is completed, so that it can be intercepted in a php file with an input form. I imagine it somehow like this:

<!-- action.php - файл проверки данных и отправки сообщения --> <form id="FeedbackFrom" name="FeedbackFrom" method="post" action="request.php"> <input type="text" id="name" name="name"> <?php if ($_GET['mail'] == 0 && $errorCode == "TooShortName") { echo "<p class=\"error\"> Введённое имя слишком короткое </p>"; } else if ($_GET['mail'] == 0 && $errorCode == "TooShortName") { echo "<p class=\"error\"> Введённое имя слишком длинное </p>"; } ?> <input type="email" id="email" name="email"> <?php if ($_GET['mail'] == 0 && $errorCode == "InvalidMail") { echo "<p class=\"error\"> Адрес электронной почты ведён неправильно </p>"; } ?> <input type ="submit" id="Submitt"/> </form> 
  • There is an exit , but it returns nothing. The error code is caught there in some pervert way; it is not clear how to catch it in the if block.
  • There are exceptions and a try-catch construct. But first, you need to create a separate class for each error, and secondly, how to catch it when request.php crashes?

    2 answers 2

     <?php $error = array(); $name = ''; $email = ''; // Если пришло значение if(isset($_POST['name'])){ $name = $_POST['name']; // Если там пусто if(!$_POST['name']){ $error['name'] = 'Пустое имя'; } } // Если пришло значение if(isset($_POST['email'])){ $email = $_POST['email']; // Если там пусто if(!$_POST['email']){ $error['email'] = 'Пустой email'; } } if(isset($_POST['name']) && isset($_POST['email']) && (!$error)){ mail($_POST['email'], 'Вы правильно заполнили форму', 'Ok' ); die(); // Тут должен быть редирект на другую страницу } ?> <form id="FeedbackFrom" name="FeedbackFrom" method="post" action="request.php"> <input type="text" id="name" name="name" value="<?php echo $name; ?>"> <?php if(isset($error['name'])){ echo '<p class="error">' . $error['name'] . '</p>'; } ?> <input type="email" id="email" name="email" value="<?php echo $email; ?>"> <?php if(isset($error['email'])){ echo '<p class="error">' . $error['email'] . '</p>'; } ?> <input type ="submit" id="Submitt"/> </form> 

      If briefly - as an option in these blocks to put a label that there are filling errors

       if ($_GET['mail'] == 0 && $errorCode == "TooShortName") { $form_invalid = true; echo "<p class=\"error\"> Введённое имя слишком короткое </p>"; } 

      And at the end of the file

       if ( empty($form_invalid) ) { // http://www.w3schools.com/php/filter_sanitize_email.asp $email = filter_var($email, FILTER_SANITIZE_EMAIL); mail($_POST['email'], 'Вы правильно заполнили форму', 'Ok' ); header('Location: action.php?mail_send=true'); die(); // Тут должен быть редирект на другую страницу, иначе при каждой перезагрузке станицы будет отправляться письмо } 

      But as for me it is better to use AJAX, it is much more convenient.

      • Thank you for your reply! Never used AJAX; is it related to javascript? I generally write PHP code-checking forms for the case when JavaScript is disabled, and so I have JS checks everything. - Bokov Gleb
      • If JavaScript is disabled, then AJAX will not work. @ I generally write PHP code-checking forms for the case when JavaScript is disabled @ JS validation does not exclude PHP validation, even with JS enabled, the user can send something wrong. - MaxWell99
      • That's right, that is why one cannot rely on AJAX either. - Bokov Gleb