私は、必須フィールドなしでユーザーデータを変更するために使用されるPHPフォームを持っています。 ユーザーがフォームのフィールドにデータを入力したかどうかを確認したい。フォームを空白のままにしたときのPHPのポスト値
私の最初のアイデアは、ユーザーが空白 フィールドの一部を残した提出し、その後、メソッドのPOSTが $ _POST [「フィールド」]変数にNULL値を入力しますクリックした場合ということでした。
したがって、$ POST ['field']変数が 'isset'関数で設定されているかどうかを確認しました。残念ながら、POSTメソッドは、フォームに指定されていない変数にNULL値を入力しません。 POSTメソッドがこれらの変数にいくつかの値を設定したので、if(isset($ [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) –
送信される空白のフォームフィールドには、空の文字列値があり、nullではありません。 'empty($ var)'を使って、変数が空であるかどうかを調べることができます。 –
この値は、ラジオと選択リスト以外のフィールドの場合は空の文字列になります。 if(!isset || strlen == 0)、ユーザデータはありません。 –