The essence of the task is as follows: it is necessary to develop a script that will check whether the required form fields are filled. If the fields are not filled, then we display a hint for what you need to fill out. In another case, we display information about the user. All of the ideas should be in the same file with the extension php. There are three functions in the code: print_form displays the form on the screen; check_form checks whether the required fields are filled in; confirm_form displays the output data

<html> <head> <title>Контактна інформація </title> </head> <body> <?php $f_name=$_POST['f_name']; $l_name=$_POST['l_name']; $email=$_POST['email']; $zip=$_POST['zip']; $object=$_POST['object']; function print_form($f_name,$l_name,$email,$zip,$object) { ?> <form action="form_start.php" method="post"> <table cellspasing="2" cellpedding="2" border="1"> <td>Ім'я</td><td><input name="f_name" type="text" value="<?php echo $f_name ?>"></td> </tr> <tr> <td>Прізвище<b>*</b></td><td><input name="l_name" type="text" value="<?php print $_name?>"</td> </tr> <tr> <td>Email адреса<b>*</b></td><td><input name="email" type="text" value="<?php print $email?>"> </td> </tr> <tr> <td>Поштовий індекс<b>*</b></td> <td><input name="zip" type="text" value="<?php print $zip?>"> </td> </tr> <tr> <td>Улюблений предмет</td> <td><input name="object" type="text" value="<?php print $object?>"> </td> </tr> </table> <input name="submit" type="submit" value="Надіслати"> <input type="reset" value="Відмінити"> </form> <? } function check_form($f_name,$l_name,$email,$zip,$object) { if(!$l_name||!$email||!$zip):echo "<h3>Помилка у заповнені форми</h3>"; if($l_name) { echo"<h3>Ви не заповнили поле<b>Прізвище</b></h3>"; } if(isset($email)) { echo "<h3>Ви не заповнили поле <b>Email адреса</b></h3>"; } if(!$zip) { echo "<h3>Ви не заповнили поле <b>Поштовий індекс</b></h3>"; } print_form($f_name,$l_name,$email,$zip,$object); else: confirm_form($f_name,$l_name,$email,$zip,$object); endif; } function confirm_form($f_name,$l_name,$email,$zip,$object) { ?> <h2>Дякуємо! Слідуюча інформація була успішно надіслана</h2> <b>Контактна інформація</b> <? $f_name=$_POST['f_name']; $l_name=$_POST['l_name']; $email=$_POST['email']; $zip=$_POST['zip']; $object=$_POST['object']; echo "<br>$f_name $l_name<br>$email<br>Поштовий індекс:$zip<br>Улюблений предмет:$object\n"; } if(!$submit): ?> <h3>Будь-ласка, введіть інформацію про себе</h3> Поля з<b>*</b>обовязкові для заповнення<p> <?php print_form("","","","","",""); else: check_form($f_name,$l_name,$email,$zip,$object); endif; ?> </body> </html> 

2 answers 2

Either JavaScript: http://www.formvalidator.net/#reg-form , or:

 <?php if(!isset($_POST['required_field'])) { echo 'Не заполнено обязательное поле'; } ?> 
  • There is no JS tag in the question, so this is not the answer. - user207618
  • @Other, Ok, then your option besides JS, and isset (). - John Doe
  • JS is useful, but you can always turn it off. Server-side model validation should be required. - teran
  • @teran Try with JS off, go to vk.com ... If the user has disabled JS, then you need to force it to enable <noscript><p>Ваш браузер не поддерживает скрипты!</p></noscript> - John Doe
  • @ VadimPedchenko does this somehow prevent the sending of an invalid post-request? - teran

In my opinion it will be easier to add to input required:

 <input name="submit" type="submit" value="Надіслати" **required**> <input type="reset" value="Відмінити" **required**> 
  • And hackers who know how to use the browser console will be very grateful to you :) - user207618
  • in my opinion it is better to study elementary php-syntax. Not even "better to learn," but at least read. - root_x Povierennyy
  • to highlight the code, use the curly braces on the editor toolbar - Alex