PHP: эхо не работает над методом POST из формы
Я немного новичок в PHP, и у меня есть некоторые трудности здесь и там. Я разрабатываю форму и хочу отобразить окно с ошибкой, если какое-либо из полей будет пустым при нажатии кнопки "Отправить". Я попробовал следующий код, но эхо все еще не появляется. Какие-либо предложения?
Код формы:
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="$_POST" action="searchEmployee.php">
<br style="clear:both">
<h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required>
</div>
<div class="form-group">
<label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label>
<input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div>
<div class="form-group">
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
</div>
</form>
</div>
</div>
</div>
PHP-код:
<?php
if (isset($_POST['submit'])) {
$required = array('name', 'surname', 'ID', 'visitorCard');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
}
}
?>
7 ответов
Вот несколько ошибок:
- Вы используете метод как
$_POST
в вашем атрибуте формы, должно быть толькоPOST
, Ваша форма не будет отправлена, потому что ваша кнопка не относится к типу отправки. должно
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor" />
Или же
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
Вы используете метод как $_POST
в вашем атрибуте формы,
Должно быть только POST
,
Итак, замените строку формы на,
<form role="form" method="POST" action="searchEmployee.php">
а также измените строку кнопки "Отправить" на
<input type="submit" name="submit" value="Sign In Visitor" class="btn btn-primary pull-right" />
Изменить это
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
к
<input type="button" id="submit" name="submit" value="submit" class="btn btn-primary pull-right" />Sign In Visitor
а также это
<form role="form" method="$_POST" action="searchEmployee.php">
к
<form role="form" method="POST" action="searchEmployee.php">
Как вы получите POST['submit']
значение, которое вы даже не установили, это означает, что ваш if-код не будет его выполнять, и поэтому он не будет отображать или предупреждать его. Для лучшей практики отладки всегда старайтесь делать это всякий раз, когда такие экземпляры происходят во время кодирования.
print_r($_POST);
Это покажет вам массив переменных POST
Менять $_POST
в POST
и положи <input type="submit"
вместо <input type = "button"
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="post" action="searchEmployee.php"> <br style="clear:both"> <h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3> <div class="form-group"> <label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required> </div>
<div class="form-group"> <label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required> </div> <div class="form-group"> <label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label> <input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div> <div class="form-group">
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
</div> </form> </div>
</div> </div>
Php Code:::searchEmployee.php
<?php if (isset($_POST['submit'])) { $required = array('name', 'surname', 'idCard', 'cardNumber');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) { if (!isset($_POST[$field])) {
$error = true;
} }
if ($error) {
echo "All fields are required."; } }
?>
Сначала подумайте кнопку изменения
type="submit"
,Второй думаю изменить массив PHP
$required = array('name', 'surname', 'idCard', 'cardNumber');
Вы должны сделать 2 изменения, как показано ниже
- Изменить тип кнопки, как это
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
- Изменить метод формы
<form role="form" method="POST" action="searchEmployee.php">