Hi all,
I’ve finally figured out how to work with arrays (in the way I want to for my current project), basic stuff probably but I’m patting myself on the back for these little accomplishments!
I have a form in which the user will be able to enter 10x numeric IDs and 10x alphanumeric Titles, grabbed with CarID[] and CarTitle[] as arrays.
What I want to do is, for each field that fails validation (say, is empty), I want to somehow link that element in the array back to the form field it was taken from and display an error.
So let’s say if the sixth CarID element has no value, place an error in that form field’s value (or add a error class to that input’s tag).
I know the index for the sixth CarID will be 5 (index starts at 0), but I don’t want to have to explicitly declare a variable or specify an index number for each input field.
It should just know which form field where the empty/invalid value came from and apply the error to that field.
[php]foreach (($CarID as $ID) && ($CarTitle as $Title)) {
if ((empty($ID)) || (empty($ID)) {
echo “This field is empty! Enter something or the form won’t submit!”;
}
}[/php]
Is it relatively straight forward?