This is probably a stupid way of doing what I want to do, so a more elegant solution to the bigger problem is definitely appreciated! However, the specific problem I am encountering is this:
I am processing a form with javascript. The form structure is as follows:
<FORM> <SECTION> Jane Doe: <INPUT type="hidden" name="user[]" id="user[]" value="Jane_Doe" /> <INPUT type="checkbox" name="preference[]" id="preference[]" value="green" /> <INPUT type="checkbox" name="preference[]" id="preference[]" value="purple" /> <SELECT name="time[]" id="time[]"> <OPTION value="AM">AM</OPTION> <OPTION value="PM">PM</OPTION> <OPTION value="Midnight">Midnight</OPTION> </SELECT> </SECTION> <SECTION> John Jacob: <INPUT type="hidden" name="user[]" id="user[]" value="John_Jacob" /> <INPUT type="checkbox" name="preference[]" id="preference[]" value="green" /> <INPUT type="checkbox" name="preference" id="preference" value="purple" /> <SELECT name="time[]" id="time[]"> <OPTION value="AM">AM</OPTION> <OPTION value="PM">PM</OPTION> <OPTION value="Midnight">Midnight</OPTION> </SELECT> </SECTION> <INPUT type="button" Value="SUBMIT" onclick="function(form_script)"/> </FORM>
Depending on a prior action by the user, a list of names is generated, with Preference and Time needing to be filled in. The form is then submitted and a mySQL table will be populated according to the user's response using PHP coding.
I am currently at a loss to how to store the form responses with Javascript. As you can see from the coding, each <SECTION>
contains the exact same coding structure. Ideally, I'd like to store each form element within an array, (e.g. user['Jane_Doe', 'John_Jacob']
) when the form is submitted, and pass that to the php script. But, I'm not sure how to create these arrays from the form elements, and would appreciate help.
I hope my question is clear.
Alternatively, if there are better ways of processing this form without using javascript arrays, I would definitely be interested in the solution!
assuming the id of your form is "form", to grab all the data from the form you will need to do this (you will need to include jquery to be able to perform this action) :
var form_data = $("#form").serialize();