Jump to content

Archived

This topic is now archived and is closed to further replies.

Tandem

form processing

Recommended Posts

I'm using a javascript file with my site that requires that i do not name my submit inputs, but i need to get the submit value so that my script executes the correct pieces of code depending on which submit input has been pressed, because some of my pages have several forms on them.

Is there any way to find which submit has been pressed, or which form it was in or something like that without me having to name my submit inputs and get there values with $_POST['submitnamegoeshere']; ?

Thanks in advnace for any help, and please say if you need more details.

Share this post


Link to post
Share on other sites
If i do that, then people could fill in a form and press the submit button from a different form and it would still process their input.

Share this post


Link to post
Share on other sites
What if i name the form rather than the submit input? Is there anyway to get a value for the form that has been submitted?

Share this post


Link to post
Share on other sites
JavaScript automatically keeps track of forms and their elements using DOM Level 0 syntax:

[code]document.forms[i].elements[i].value[/code]

Elements are inputs, selects, and textareas.  So, if the form you're talking about is the first form on your page, and you want to get to the fourth form element, calling [code]document.forms[0].elements[3][/code] will work.

Unfortunately, using this method isn't very efficient if you're doing a lot with forms as JavaScript will walk the DOM every time you explicitly use that syntax.  If you're going to be using the same element a lot, store it in a variable (something like [code]var elem = document.forms[0].elements[1][/code] for the second element of the first form, for example).  That will cut down on the overhead incurred by the Level 0 syntax as the script won't traverse the hierarchy every time it's called.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.