Is there a way to return a list / array of all the field names that are contained within a form? I.e if i made a form with 10 fields (‘name’, ’email’ etc…) on submit i can determine what the element names are?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In JavaScript we can get the
nameattribute of each form element like this:This gathers the
nameattribute of eachinputortextareaelement in the form and puts them in an array,names.Note that
.on()is new as of jQuery 1.7 and in this case is the same as using.bind(): http://api.jquery.com/onIn PHP you can loop through each of the
$_GETor$_POSTvariables:And again, the
$namesvariable is an array of all the names of form elements.Update
If you want to create an associative array of
names:valuesin JS you can do this:Now you can access the
namesvariable like this: