Jump to content

form processing


Tandem

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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.