0
I am validating a form and I face a problem that I cannot solve. First, I make a foreach and check if there are any $_POST
empty, if any, executes: $error[$key] = "*";
. Then, below the input, I check if the user clicked on Submit and whether the variable $error[$key]
was set, if yes, prints "*"; <br>
, but nothing happens.
foreach:
foreach ($_POST as $key => $value) {
if(empty($_POST[$value])) {
$error[$key] = "*";
$valida = false;
}
}
input:
<input type="text" class="demoInputBox" name="firstName" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>">
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($error['firstName'])) echo "<label style = 'color: #ff0000'>" . $error['firstName'] . "</label>"; ?>
It hasn’t changed at all, I think it’s that because $_POST is an array, it doesn’t matter if it looks for key or value, both are empty
– Tales Rodrigues
Are you sure, man? I took the test here and it worked right.
– Vitor Adriano
I discovered the mistake, I was putting the foreach after the input... the code is right, I was stupid. Thanks for answering and sorry for wasting your time
– Tales Rodrigues