我有一个php表单,用于更改没有必填字段的用户数据。 我想检查用户是否在表单的字段中输入数据。PHP当表单空白时发布值
我最初的想法是,如果用户点击提交留下了一些 字段为空,那么POST方法将在 $ _POST ['field']变量中输入NULL值。
因此,我检查了是否使用'isset'函数设置了$ POST ['field']变量。不幸的是,看起来,POST方法不会在表单中未指定的变量中输入NULL值。这使得if(isset($ [POST])语句成为真实的,因为POST方法确实为这些变量设置了一些值。
有关如何检查用户是否在字段中输入数据的任何想法? 这里是我的代码:
<?php
include 'core/init.php';
//logged_in_redirect();
include 'includes/overall/admin_start.php';
?>
<h2>Employee information: </h2>
<?php
echo "Username: ", $_SESSION['user_data']['username'], "<br>";
echo "First name: ", $_SESSION['user_data']['first_name'], "<br>";
echo "Last name: ", $_SESSION['user_data']['last_name'], "<br>";
echo "Email: ", $_SESSION['user_data']['email'], "<br>";
echo "City: ", $_SESSION['user_data']['city'], "<br>";
echo "Type: ", $_SESSION['user_data']['type'], "<br>";
?>
<h2> Alter information: </h2>
<?php
if(empty($_POST) === true){
echo "print";
}
else{
if(isset($_POST['username'])){
if(user_exists($_POST['username']) === true){
$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\'
is already taken.';
}
if(preg_match("/\\s/", $_POST['username']) == true){
$errors[] = 'Your username must not contain any spaces.';
}
if(strlen($_POST['username']) > 30){
$errors[] = 'The size of one of your entries is not acceptable.
Entry size must be smaller than 30 characters.';
}
}
if(isset($_POST['password'])){
//echo $_POST['password'];
if(strlen($_POST['password']) < 6){
$errors[] = 'Your password size is not acceptable. Password size
must be between 6 and 30 characters.';
}
if($_POST['password'] !== $_POST['repeat_pass']){
$errors[] = 'Your password entries do not match.';
}
if(strlen($_POST['password']) > 30){
$errors[] = 'The size of one of your entries is not acceptable.
Entry size must be smaller than 30 characters.';
}
}
if(isset($_POST['email'])){
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'A valid email address is required.';
}
if(email_exists($_POST['email']) === true){
$errors[] = 'Sorry, the email \'' . $_POST['email'] . '\' is
already in use.';
}
if(strlen($_POST['email']) > 30){
$errors[] = 'The size of one of your entries is not acceptable.
Entry size must be smaller than 30 characters.';
}
}
if(isset($_POST['type'])){
if(($_POST['type'] !== "1") && ($_POST['type'] !== "2") &&
($_POST['type'] !== "3")){
$errors[] = 'Please insert a correct number in the field type';
}
}
if(isset($_POST['city'])){
if(city_exists($_POST['city']) === false){
$errors[] = 'Sorry, there is no shop in the specified city';
}
if(strlen($_POST['city']) > 30){
$errors[] = 'The size of one of your entries is not acceptable.
Entry size must be smaller than 30 characters.';
}
}
}
?>
<?php
if(isset($_GET['success']) && empty($_GET['success'])){
echo 'You have altered the employee successfully';
}
else
{
if(empty($_POST) === false && empty($errors) === true)
{
if(isset($_POST['username'])){$register_data['username'] =
$_POST['username'];}
if(isset($_POST['password'])){$register_data['password'] =
$_POST['password'];}
if(isset($_POST['first_name'])){$register_data['first_name'] =
$_POST['first_name'];}
if(isset($_POST['last_name'])){$register_data['last_name'] =
$_POST['last_name'];}
if(isset($_POST['email'])){$register_data['email'] =
$_POST['email'];}
if(isset($_POST['city'])){$register_data['city'] = $_POST['city'];}
if(isset($_POST['type'])){$register_data['type'] = $_POST['type'];}
alter_user($register_data);
header('Location: alter_employee.php?success');
}
else{echo output_errors($errors);}
?>
<form action="" method="post">
<ul>
<li>
Username:<br>
<input type="text" name="username">
</li>
<p><li>
Password:<br>
<input type="password" name="password">
</li></p>
<p><li>
Repeat password*:<br>
<input type="password" name="repeat_pass">
</li></p>
<p><li>
First name:<br>
<input type="text" name="first_name">
</li></p>
<p><li>
Last name:<br>
<input type="text" name="last_name">
</li></p>
<p><li>
Email:<br>
<input type="text" name="email">
</li></p>
<p><li>
City:<br>
<input type="text" name="city">
</li></p>
<p><li>
Type (1,2,3):<br>
<input type="text" name="type">
</li></p>
<p><li>
<input type="submit" value="Register">
</li></p>
</ul>
</form>
<?php
}
include 'includes/overall/end.php'; ?>
允许用户使用他们所需的[密码/短语](https://xkcd.com/936/)。 [不限制密码。](http://jayblanchard.net/security_fail_passwords.html) –
提交的空白表单字段的空字符串值不为空。您可以使用'empty($ var)'来确定变量是否为空。 –
对于收音机和选择列表以外的字段,该值将是一个空字符串。如果(!isset || strlen == 0)则没有用户数据。 –