N
N
Natalia2017-01-20 18:20:31
PHP
Natalia, 2017-01-20 18:20:31

How to do the correct processing of the form in this case?

Good afternoon! I have a question about form processing.
I write the following php code:

/*сначала задаю переменным "пустые" значения.*/

$lastnameErr = $firstnameErr = $middle_nameErr = "";
$lastname = $firstname = $middle_name = "";

$arrayErr = "";
$arrayErr = array("lastnameErr", "firstnameErr", "middle_nameErr");

/*далее делаю обработку формы*/

if ($_SERVER['REQUEST_METHOD'] == "POST") { 
//проверяем запрощен ли скрипт post запросом, если да - обрабатываем так:     
 if (empty($_POST["lastname"])) {
    $lastnameErr = "Не заполнено обязательное поле";
  } else {
    $lastname = test_input($_POST["lastname"]);
  }
  if (empty($_POST["firstname"])) {
    $firstnameErr = "Не заполнено обязательное поле";
  } else {
    $firstname = test_input($_POST["firstname"]);
  }
  if (empty($_POST["middle_name"])) {
    $middle_nameErr = "Не заполнено обязательное поле";
  } else {
    $middle_name = test_input($_POST["middle_name"]);
  }
}
if (!empty($arrayErr)){
    echo "Спасибо, Ваши данные успешно сохранены!";
 }
/* доп пояснение: функция test_input отвечает за валидацию */

The HTML form looks like this:
<label>Фамилия:</label>
    <input type="text" size="40" name="lastname" value= "<?php echo $lastname ?>" >
    <span class="error">* <?php echo $lastnameErr;?></span>
    <br><br>
 <label>Имя:</label>
    <input type="text" size="40" name="firstname" value= "<?php echo $firstname ?>" >
    <span class="error">* <?php echo $firstnameErr;?></span>
    <br><br>
 <label>Отчество:</label>
    <input type="text" size="40" name="middle_name" value= "<?php echo $middle_name ?>" >
    <span class="error">* <?php echo $middle_nameErr;?></span>
    <br><br>
 <input type="submit" name="submit" value="Сохранить">

It is necessary that the contents of the $arrayErr array be checked after the submit button is pressed.
And only if the values ​​of all its variables are empty, the message "Thank you, your data was successfully saved!" was displayed to the client.
If at least one variable of this array has any value other than empty, then such a message does not need to be displayed, only the line "Required field not filled" is displayed.
If there is another way to implement this idea, please let me know.
Thanks in advance for your reply.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2017-01-20
@ThunderCat

Listen, php7 is already out, php4 has been out of date for 10 years, WHERE DOES ALL THIS GARBAGE FROM THE 90s come from??? Is it really so difficult to read about OOP and MVC, well, even if MVC is not immediately, but at least the concept of objects, you will probably also use such shitty code in a working project? You need to look people in the eyes somehow! Not ashamed?

V
Vladimir Merk, 2017-01-20
@VladimirMerk

Maybe so?

$arrayErr = array();

/*далее делаю обработку формы*/

if ($_SERVER['REQUEST_METHOD'] == "POST") { 
//проверяем запрощен ли скрипт post запросом, если да - обрабатываем так:     
 if (empty($_POST["lastname"])) {
    $arrayErr['lastnameErr'] = "Не заполнено поле фамилия";
  } else {
    $lastname = test_input($_POST["lastname"]);
  }
  if (empty($_POST["firstname"])) {
    $arrayErr['firstnameErr'] = "Не заполнено поле имя";
  } else {
    $firstname = test_input($_POST["firstname"]);
  }
  if (empty($_POST["middle_name"])) {
    $arrayErr['middlenameErr'] = "Не заполнено поле отчество";
  } else {
    $middle_name = test_input($_POST["middle_name"]);
  }
}
if (!empty($arrayErr)){
    echo "Спасибо, Ваши данные успешно сохранены!";
 }
/* доп пояснение: функция test_input отвечает за валидацию *

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question