Reputation: 167
I have a php form used to alter user data with no mandatory fields. I want to check if the user entered data in the fields of the form or not.
My initial idea was that if the user clicked submit having left some of the fields blank, then the method POST would enter the NULL value in the $_POST['field'] variable.
Therefore I checked if the $POST['field'] variables are set with the 'isset' function. Unfortunately, as it seems, the POST method does not enter the NULL value in the variables that were not specified in the form. This makes the if(isset($[POST)) statements true since the POST method did set some value to these variables..
Any ideas about how I can check if the user entered data in the fields? Here is my code:
<?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'; ?>
Upvotes: 0
Views: 6318
Reputation: 1
Non of above seemed to work for me. However, I did this:
if($_POST['foo'] != "null")
and that did the trick. I found this because I was trying to
echo "Value is ".$_POST['foo'].".
"
and kept getting
"Value is null"
when I wasn't passing a real string.
Upvotes: -1
Reputation: 34426
For example you could do this:
$foo = empty($_POST['foo']) ? 'default value' : $_POST['foo'];
You check to see if it the variable is empty and provide a default value, else populate the variable with the posted value.
Alternatively, because the $_POST
variable is expected to be set by the form submission, you can just check if the value is "falsey":
$foo = $_POST['foo'] ?: 'default value';
Because a string-type value of 0
is considered "falsey", it will be more accurate to check the string length. Optionally if you want to check if a field is blank or contains only spaces, you can trim()
before calling strlen()
or call ctype_space()
after strlen()
.
Upvotes: 1
Reputation: 658
Avoid using empty
as it also checks falsy conditions like 0
and "0"
and only using isset
will not catch empty strings. It's best to check against an expected value for empty input, e.g.
if (isset($_POST['fieldName']) && trim($_POST['fieldName']) !== "") {
echo "There's something here!";
}
Upvotes: 3