We help IT Professionals succeed at work.

Multiple forms on a single page, how do you tell which form has been submitted?

jasch2244
jasch2244 asked
on
I know this is something that is probably pretty simple but... if you have multiple forms on a single page that are hidden/shown with jquery, javascript or whatever what is the best method for determining which form was submitted. Cfif statements, switch statements and what are you looking for in the form (hidden id, submit button name) etc.?

Comment
Watch Question

Commented:
Hi,

In your form tag, set the name field differently for each form.
This is particularly useful for javascript.
Also you can name the submit buttons differently if that is easier for you.
Then on the page you submit to you can reference by the form name or button name.

<form name="form1" action..........><input type="submit" name="submit1"></form>
<form name="form2" action..........><input type="submit" name="submit2"></form>
<form name="form3" action..........><input type="submit" name="submit3"></form>

Hope this helps.

Author

Commented:
And you would write what on the action page FORM.submit1 etc.? and it will take?
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
I would use different 'action' pages.  Then if they need to end up on the same page, redirect them.
Commented:
Hi,

Yes, on the action page you could use cfif to handle processing of different forms.

<cfif isdefined("form.submit1")>
Process form1................
<cfelseif isdefined("form.submit2")
Process form2................
</cfif>
etc..........

Author

Commented:
If i try and reference the #FORM.submit1# I get an undefined error. In other words it is not picking up the name of the submit button. Nor will it pick up the name of the form either.

Commented:
Hi ,
Can you output this variable?
#form.formfields#

This should give you a list of all the fields.  Although, if they do not click the submit button it will not come through.

Author

Commented:
Ahh yeah.... I did not have form set to post... I'm an idiot! Thanks for your help

Author

Commented:
Instead of the elseif's can I use your method on switch statements? Which is quicker or more efficient?
CERTIFIED EXPERT
Commented:
cfswitch can't do  isDefined("form.xxxx"), it's to test a value of the form field.  So I would stick with CFIF, CFELSEIF.

Just to be a bit clearer, I would name your submit buttons after the action they are to perform... for example, if one form is to save the product and another form is to save the client or delete the product, you can name your submit buttons as such...


<input type="submit" name="doSaveProduct" value=" Save Product">


<input type="submit" name="doDeleteProduct" value=" Delete the Product">


<input type="submit" name="doSaveCustomer" value=" Save Customer">


Then your CFIF statements test for the name...

<cfif isDefined("form.doSaveProduct")>

etc....


Also note that you can use exactly the same action page for all these actions, because the form's submit button will be in charge of executing the correct action block.   You could also submit the page to itself and place the CFIF statements at the top of the page, this is a very convenient and powerful way to process a form.  After the action processes, use CFLOCATION to redirect to the next page (you can have a different cflocaton for each CFIF block).

Explore More ContentExplore courses, solutions, and other research materials related to this topic.