lundi 19 avril 2021

Anyone know how to do a form input with an array checkbox element in php?

I have a form with a list of choices

<form ... >
<input tyle="text" name="carname[]" value="(printed by a "for" loop from database list)">  
<input tyle="text" name="carfeature[]" value="">    <!-- <- optional -->
<input tyle="text" name="carcolor[]" value="">      <!-- <- optional -->
<input tyle="checkbox" name="carpref[]" value="Y">  <!-- <- optional -->
<input type="submit" value="submit">
</form>

The list of cars is 70 rows long. When I process the results, I have 4 arrays (carname[], carfeature[], carcolor[], carpref[]. The idea is that the user would read down the list and enter features, colors in whatever cars they can think of, then check the checkbox on the ones they really prefer. So, array carname[] is 70 key/value pairs long with a car name value with every key.

I receive 3 arrays (name/feature/color) with 70 key/value pairs (values empty or not), but the problem is with the checkbox array, I only receive an array containing just the checked pref. So if they checked a preference on 5 car names.. my checkbox array is 5 key/value pairs long. There is now way that I can match the checkbox against which car they were preferring.

Thanks in advance if someone knows how to get around this issue?




Aucun commentaire:

Enregistrer un commentaire