In this form I created, it checks if the fields are set and are empty. I made a class with two methods, areSet()
as isset()
, and areEmpty()
as empty()
. In the areEmpty()
method, it is supposed to display "Empty" if all the fields are empty, but even when all the fields are filled up, it still displays "Empty". Is it because of the implode()
function that I used? I need someone's help to point out the wrong with my code.
HTML
<form action="validate.php" method="post">
<input type="text" name="firstname" placeholder="First Name" autocomplete="off"><br>
<input type="text" name="lastname" placeholder="Last Name" autocomplete="off"><br>
<input type="text" name="email" placeholder="Email" autocomplete="off"><br>
<input type="text" name="username" placeholder="Username" autocomplete="off"><br>
<input type="password" name="password" placeholder="Password" autocomplete="off"><br>
<input type="password" name="passwordAgain" placeholder="Confirm password" autocomplete="off"><br>
<input type="submit" value="Submit">
</form>
validate.php
<?php
require 'include/class.php';
if(Input::areSet('firstname','lastname','email','username','password','passwordAgain')) {
if(Input::areEmpty('firstname','lastname','email','username','password','passwordAgain')) {
echo 'Empty';
} else {
echo 'Not empty';
}
}
class.php
<?php
class Input {
// isset()
public static function areSet(...$fields) {
$setFieldsArray = [];
foreach($fields as $field) {
$setFieldsArray[] = $_POST[$field];
}
return isset($setFieldsArray);
}
// empty()
public static function areEmpty(...$fields) {
$emptyFieldsArray = [];
foreach($fields as $field) {
$emptyFieldsArray[] = empty($_POST[$field]);
}
return implode(' || ', $emptyFieldsArray);
}
}