PHP 文件中的 $_POST 数组显示空结果
$_POST Array in PHP file is Showing Empty Results
我正在使用使用 bootstrap 4 创建的简单表单。我正在使用 php 文件进行服务器验证,但它没有收到来自表单的任何 post 条目.
这是我的表单,它有一个客户端验证,可以通过删除 "required" 表单输入字段来暂时禁用以测试服务器端验证。服务器端验证不起作用但是因为它使用 $_POST 不显示或保留来自的任何传入值。
这是我的表格:
<form role="form" action="form_handling.php" method="post" id="elegantForm" class="needs-validation" novalidate>
<!-- Name Group -->
<div class="form-group">
<label for="name">Full Name:</label>
<input type="text" class="form-control" id="fullname" aria-describedby="nameHelp" placeholder="Enter full name" required>
<div class="invalid-feedback">
Please enter full name.
</div>
</div>
<!-- Email Group -->
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="emailaddress" aria-describedby="emailHelp" placeholder="Enter email" required>
<div class="invalid-feedback">
Please enter valid email address.
</div>
</div>
<!-- Telephone Group -->
<div class="form-group">
<label for="phone">Telephone:</label>
<input type="tel" class="form-control" id="phoneNumber" aria-describedby="phonelHelp" placeholder="xxx-xxx-xxxx" required>
<div class="invalid-feedback">
Please enter telephone number.
</div>
</div>
<!-- Telephone Group -->
<div class="form-group">
<label for="tel">Message:</label>
<textarea type="textarea" class="form-control" id="messageBox" aria-describedby="messagelHelp" placeholder="Enter message here" maxlength="6000" rows="6" required></textarea>
<div class="invalid-feedback">
Please enter a message.
</div>
</div>
<!-- Submit Button -->
<button type="submit" class="btn btn-primary">Submit</button>
</form>
----------------------------------------
And here is the PHP file:
<?php
$errors = array(); // array to store validation errors
$data = array(); // array to store incoming "post" data
if (empty($_POST['name']))
$errors['name'] = 'Name is required.';
if (empty($_POST['email']))
$errors['email'] = 'Email is required.';
if (empty($_POST['phone']))
$errors['phone'] = 'Phone is required.';
if (empty($_POST['message']))
$errors['message'] = 'Message is required.';
// return a response ===========================================================
if ( ! empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}
print_r($_POST);
var_dump($_POST);
?>
您忘记输入 "name" 属性。
因为元素 ID 未通过 POST 方法传递。您需要设置表单元素的名称
<input type="tel" class="form-control" name="phoneNumber" id="phoneNumber" aria-describedby="phonelHelp" placeholder="xxx-xxx-xxxx" required>
我正在使用使用 bootstrap 4 创建的简单表单。我正在使用 php 文件进行服务器验证,但它没有收到来自表单的任何 post 条目.
这是我的表单,它有一个客户端验证,可以通过删除 "required" 表单输入字段来暂时禁用以测试服务器端验证。服务器端验证不起作用但是因为它使用 $_POST 不显示或保留来自的任何传入值。
这是我的表格:
<form role="form" action="form_handling.php" method="post" id="elegantForm" class="needs-validation" novalidate>
<!-- Name Group -->
<div class="form-group">
<label for="name">Full Name:</label>
<input type="text" class="form-control" id="fullname" aria-describedby="nameHelp" placeholder="Enter full name" required>
<div class="invalid-feedback">
Please enter full name.
</div>
</div>
<!-- Email Group -->
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="emailaddress" aria-describedby="emailHelp" placeholder="Enter email" required>
<div class="invalid-feedback">
Please enter valid email address.
</div>
</div>
<!-- Telephone Group -->
<div class="form-group">
<label for="phone">Telephone:</label>
<input type="tel" class="form-control" id="phoneNumber" aria-describedby="phonelHelp" placeholder="xxx-xxx-xxxx" required>
<div class="invalid-feedback">
Please enter telephone number.
</div>
</div>
<!-- Telephone Group -->
<div class="form-group">
<label for="tel">Message:</label>
<textarea type="textarea" class="form-control" id="messageBox" aria-describedby="messagelHelp" placeholder="Enter message here" maxlength="6000" rows="6" required></textarea>
<div class="invalid-feedback">
Please enter a message.
</div>
</div>
<!-- Submit Button -->
<button type="submit" class="btn btn-primary">Submit</button>
</form>
----------------------------------------
And here is the PHP file:
<?php
$errors = array(); // array to store validation errors
$data = array(); // array to store incoming "post" data
if (empty($_POST['name']))
$errors['name'] = 'Name is required.';
if (empty($_POST['email']))
$errors['email'] = 'Email is required.';
if (empty($_POST['phone']))
$errors['phone'] = 'Phone is required.';
if (empty($_POST['message']))
$errors['message'] = 'Message is required.';
// return a response ===========================================================
if ( ! empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}
print_r($_POST);
var_dump($_POST);
?>
您忘记输入 "name" 属性。
因为元素 ID 未通过 POST 方法传递。您需要设置表单元素的名称
<input type="tel" class="form-control" name="phoneNumber" id="phoneNumber" aria-describedby="phonelHelp" placeholder="xxx-xxx-xxxx" required>