Can you help write the handler? "Is it entered in the field, if yes, then we do it, and if not, then we give an error."
|
2 answers
Here is the file 1.php
<?php if($_POST['var']!=null){ // тут что-то выполняется... }else{ exit("ошибка"); } ?>
Here is the form in 1.html:
<html> <body> <form method="POST" action="1.php"> <input type="text" name="var"> <input type="submit"> </form> </body> </html>
- And if I have, for example, in the form of 2 fields, then how to write the script? `<? php if ($ _ POST ['var1']! = null) {// something is done here ...} else {exit (" error "); } if ($ _ POST ['var2']! = null) {// something is being executed here ...} else {exit ("error"); }?> `So? - cnofss
- Could be so. Observe the logic of the script: If var1 is not Null (that is, there is something in the variable), then the piece "// something is executed here ..." is executed. If var1 is empty, then "error" is output and the script is not executed. , accordingly the second block with IF will not even be processed. There is such an option: <pre> <? Php if ($ _ POST ['var1']! = Null AND $ _POST ['var2']! = Null) {// something is done here ...} else {exit ("Both variables are empty"); }?> </ pre> - Andrey Surzhikov
|
php
<?php /* Осуществляем проверку вводимых данных и их защиту от враждебных скриптов */ $name = htmlspecialchars($_POST["name"]); $phone = htmlspecialchars($_POST["phone"]); $email = htmlspecialchars($_POST["email"]); /* Устанавливаем e-mail адресата */ $myemail = "Ваше мыло"; /* Проверяем заполнены ли обязательные поля ввода, используя check_input функцию */ $name = check_input($_POST["name"], "<center><b><span style='color:red; font: 15px Arial';>Введите ваше имя!</span><p>"); $phone = check_input($_POST["phone"], "<center><b><span style='color:red; font: 15px Arial';>Вы забыли написать номер телефона!</span><p>"); $email = check_input($_POST["email"], "<center><b><span style='color:red; font: 15px Arial';>Введите ваше имя!</span><p>"); /* Проверяем правильно ли записано имя */ if (!preg_match("/^[az а-яё 0-9]{2,30}$/iu", $name)) { show_error("<br><center><b><span style='color:red; font: 15px Arial';> Убедитесь что Имя содержит от 2 до 30 символов</span><p>"); } /* Проверяем правильно ли записан e-mail */ if (!preg_match( '#^[0-9a-z_\-\.]+@[0-9a-z\-\.]+\.[az]{2,6}$#i', $email)) { show_error("<br><center><b><span style='color:red; font: 15px Arial';> Пожалуйста, введите корректный адресс электронной почты. E-mail должен иметь вид: <span style='color:blue';>user@somehost.com</span></span><p>"); } /* Проверяем правильно ли записан номер телефона пользователя*/ if (!preg_match( '/[0-9]{5,15}/', $phone)) { show_error("<br><center><b><span style='color:red; font: 15px Arial';> Не правильно заполнено поле номер телефона. Номер телефона: <span style='color:blue';> 8 063 077 00 00(без пробелов)</span></span><p>"); } $to = "Ваше мыло"; $subject = "Сообщение от посетителя сайта"; $message = "\nИмя:$name \nТелефон:$phone \nПочта:$email"; mail ($to,$subject,$message, "Content-Type: text/plain; charset=utf-8 \r\n") or print "Не могу отправить сообщение."; ?> <?php /* Если при заполнении формы были допущены ошибки сработает следующий код: */ function check_input($data, $problem = "") { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?> <html> <body> <p><center><b><span style='color:red; font: 15px Arial';>Пожалуйста, исправьте следующую ошибку:</span></p> <?php echo $myError; { echo "<a href=../index.html#form> Вернуться и правильно заполнить форму.</a>"; } ?> </body> </html> <?php exit(); } ?>
html
<form id="form" action="php/form.php" method="post" > <div><input type="text" name="name" placeholder="ВАШЕ ИМЯ" /></div> <div><input type="text" name="phone" placeholder="ВАШ ТЕЛЕФОН"/></div> <div><input type="text" name="email" placeholder="ВАШ E-MAIL"/></div> <input type="submit" class="submit" value="ЗАКАЗАТЬ"/> </form>
|