我有一个用于更改用户数据的PHP表单,没有必填字段。 我想检查用户是否在表单的字段中输入数据。
我最初的想法是,如果用户点击提交已经离开了一些 如果字段为空,则POST方法将输入NULL值 $ _POST ['field']变量。
因此我检查了$ POST ['field']变量是否使用'isset'功能设置。不幸的是,看起来,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'; ?>
答案 0 :(得分:1)
例如,你可以这样做:
$foo = (!isset($_POST['foo']) || empty($_POST['foo'])) ? 'default value' : $_POST['foo'];
您检查它是否未设置并提供默认值,否则使用已发布的值填充变量。
答案 1 :(得分:1)
避免使用empty
,因为它还会检查0
和"0"
等虚假条件,只使用isset
不会捕获空字符串。最好检查空输入的预期值,例如
if (isset($_POST['fieldName']) && trim($_POST['fieldName']) !== "") {
echo "There's something here!";
}
答案 2 :(得分:-1)
以上都不对我有用。但是,我这样做:
if($ _ POST ['foo']!=“空”)
这就是窍门。我发现这个是因为我试图
echo“值是”。$ _ POST ['foo']。“。
”
并不断得到
“值为空”
当我没有传递真实的字符串时。