Answer the question
In order to leave comments, you need to log in
How to return the key of an array element?
There is an array $_POST.
How to get element key from this array?
for example, there is an element $_POST['name']
How to display the key "name" without resorting to foreach ?
---------------
$_POST comes from the user via a form. After validating the data on the server, I need to get the keys of those elements (key == id of the field in the form) that did not pass the validation and send them back (ajax) in order to match these keys with the form elements that did not pass the validation and apply certain actions to these fields .
Answer the question
In order to leave comments, you need to log in
An array key has no key in this array. He is the key.
You do something like this:
if (false === Validator::isValidName($_POST['name'])){
$errors['name'] = 'Введите человеческое имя!';
}
<label>
<input type="name" name="name" value="" />
<span class="error"><?= $errors['name'] ?></span>
</label>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question