I’m rewriting a form that was using variables with numbers instead of an array for the names and ids in the input elements. (e.g. agency1, agency2, agency3…)
I know I can do something like:
for ($i = 0; $i < $AMOUNT_OF_AGENCIES; $i++)
{
<label for="agency<?= $i ?>">Agency Name</label><input type="text" name="agency[]" id="agency<?= $i ?>">
}
to output the input elements that I need and within an easier to use array, but I would also like to have data retention in case the form errors out because of a validation error in another part of the form.
Can I do this?
<input type="text" name="agency[]" id="agency<?= $i ?>" value="<?= htmlspecialchars($_POST['agency[$i]']) ?>">
?
(note: my php syntax may be incorrect in some places, php is not my first language, please forgive me)