Answer the question
In order to leave comments, you need to log in
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 отвечает за валидацию */
<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="Сохранить">
Answer the question
In order to leave comments, you need to log in
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?
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 questionAsk a Question
731 491 924 answers to any question