Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<CFSWITCH> and FORMS 2

Status
Not open for further replies.

bluesauceuk

Programmer
Jan 1, 2001
73
GB
Hello,

I would like to have a page that does a number of functions in one page. How would I go about it?

I have a page... this page will show results from a query in a list. At the end I want to have a form to add in a new record.

I don't want to use fusebox stuff - 'coz it too complicated!

so here we go..

PAGE TITLE

RESULT 1
RESULT 2
RESULT 3

{form action="send to self")
Add a new record

(/form)

I guess that's what I want to do...

Thanks in advance
 
The CFSWITCH is a decent solution, if you have a lot of different functions.

Something like:
Code:
<CFIF IsDefined(&quot;FORM.submit&quot;) AND Len(Trim(FORM.submit)) GT 0 AND IsDefined(&quot;FORM.RecordNumber&quot;)>

	<CFSWITCH expression=&quot;#LCase(Trim(FORM.submit))#&quot;>
	<CFCASE value=&quot;add&quot;>
	   Adding a new record
	   <!--- query or code to add record --->
	</CFCASE>
	<CFCASE value=&quot;delete&quot;>
	   Deleting record <CFOUTPUT>#FORM.RecordNumber#</CFOUTPUT>
	   <!--- query or code to delete record --->
	</CFCASE>
	<CFCASE value=&quot;edit&quot;>
	   Editing record <CFOUTPUT>#FORM.RecordNumber#</CFOUTPUT>
	   <!--- query or code to edit record --->
	</CFCASE>
	</CFSWITCH>
</CFIF>

<table border=&quot;1&quot;>
<CFOUTPUT>
<form action=&quot;#GetFileFromPath(GetBaseTemplatePath())#&quot; method=&quot;post&quot;>
<TR>
<TD>Record 1 <INPUT TYPE=&quot;hidden&quot; NAME=&quot;RecordNumber&quot; VALUE=&quot;1&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Add&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Delete&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Edit&quot; /></TD>
</TR>
</form>
</CFOUTPUT>
</table>

If you just have one or maybe two functions, you could just use a CFIF instead of the CFSWITCH. It's up to you.

Hope it helps,
-Carl
 
Hey Carl,

Could I also do this through the URL string i.e. if I wanted to display something - but only sometimes...

so somefilename.cfm would do as normal..

but say.. somefilename.cfm?method=showusersloggedin

would do as above - but would also include the showusersloggedin... using a CFINLUDE...

Cheers,


Mark
 
Bwaaaaah... uh... yeah... I think. I'm not quite catching all of what you're asking, but you can certainly use the same method to act on URL variables, or whatever you like.

The trick is that you aren't really supposed to pass a query string in a FORM action. In other words, something like:
Code:
<FORM action=&quot;somefilename.cfm?method=showusersloggedin&quot; ...>
would be a no-no.

The proper way to handle the above would be to have the variables be hidden fields:
Code:
<FORM action=&quot;somefilename.cfm&quot; ...>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;method&quot; VALUE=&quot;showusersloggedin&quot;>

If your FORM method is GET, of course you'd get at the variable with #URL.method# anyway. And if you're using POST, you'd simply get at it with #FORM.method#.

But, yeah, you can certainly use it to include files:
Code:
<CFSWITCH expression=&quot;#URL.method#&quot;>
   <CFCASE value=&quot;showusersloggedin&quot;>
      <CFINCLUDE template=&quot;showusersloggedin.cfm&quot;>
   </CFCASE>
      :

Does that answer your question at all?
-Carl
 
Ooops!

I think we are getting our wires crossed!

The single page will do a number of functions. On the page will the the list of results from a query each of these will have EDIT and DELETE.

Then at the end of the results the form for ADDING. This form should submit to itself.

Now, back to the results in the middle.

Description here edit delete

the <A HREF> on the edit button should submit to the same page.. but with a URL variable such as thisfile.cfm?method=edit&uniqueid=3452

Does that help?

Mark ;-)
 
Ahhh... yes... you can do that. Or your edit and delete could be form buttons. Doesn't matter. The advantage to them being form buttons is you only need to check FORM.submit in your CFIF at the top. Otherwise you need to check URL.method and URL.uniqueid.

Remember, you can have many forms on a single page... so something like:
Code:
<CFIF IsDefined(&quot;FORM.submit&quot;) AND Len(Trim(FORM.submit)) GT 0>

	<CFSWITCH expression=&quot;#LCase(Trim(FORM.submit))#&quot;>
	<CFCASE value=&quot;add&quot;>
	   Adding a new record
	   <!--- query or code to add record --->
	</CFCASE>
	<CFCASE value=&quot;delete&quot;>
	   <CFIF IsDefined(&quot;FORM.RecordNumber&quot;)>
	      Deleting record <CFOUTPUT>#FORM.RecordNumber#</CFOUTPUT>
	   	  <!--- query or code to delete record --->
	   </CFIF>
	</CFCASE>
	<CFCASE value=&quot;edit&quot;>
	   <CFIF IsDefined(&quot;FORM.RecordNumber&quot;)>
	      Editing record <CFOUTPUT>#FORM.RecordNumber#</CFOUTPUT>
	   	  <!--- query or code to edit record --->
	   </CFIF>
	</CFCASE>
	</CFSWITCH>
</CFIF>

<CFSET thisPage = GetFileFromPath(GetBaseTemplatePath())>

<table border=&quot;1&quot;>
<CFOUTPUT>

<TR>
<form action=&quot;#thisPage#&quot; method=&quot;post&quot;>
<TD>Record 1 <INPUT TYPE=&quot;hidden&quot; NAME=&quot;RecordNumber&quot; VALUE=&quot;1&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Delete&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Edit&quot; /></TD>
</form>
</TR>

<TR>
<form action=&quot;#thisPage#&quot; method=&quot;post&quot;>
<TD>Record 2 <INPUT TYPE=&quot;hidden&quot; NAME=&quot;RecordNumber&quot; VALUE=&quot;2&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Delete&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Edit&quot; /></TD>
</form>
</TR>

<TR>
<form action=&quot;#thisPage#&quot; method=&quot;post&quot;>
<TD>Record 3 <INPUT TYPE=&quot;hidden&quot; NAME=&quot;RecordNumber&quot; VALUE=&quot;3&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Delete&quot; /></TD>
<TD><INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Edit&quot; /></TD>
</form>
</TR>

</CFOUTPUT>
</table>

<CFOUTPUT>
<form action=&quot;#thisPage#&quot; method=&quot;post&quot;>
<INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Add&quot; />
</form>
</CFOUTPUT>

works just fine (notice the individual forms). The trick is the hidden field for &quot;RecordNumber&quot;. Each form submits a different value, so you can act on it in the CFSWITCH.


Your way you'd want to do a lot of checks up front to validate the variables... but it's certainly yet another way to do it:
Code:
<CFIF IsDefined(&quot;FORM.submit&quot;) AND CompareNoCase(FORM.submit,&quot;add&quot;) EQ 0>
	   Adding a new record
	   <!--- query or code to add record --->
	   
<CFELSEIF IsDefined(&quot;URL.method&quot;) AND Len(Trim(URL.method)) GT 0 AND IsDefined(&quot;URL.uniqueid&quot;) AND Len(Trim(URL.uniqueid)) GT 0>

	<CFSWITCH expression=&quot;#LCase(Trim(URL.method))#&quot;>
	<CFCASE value=&quot;delete&quot;>
       Deleting record <CFOUTPUT>#URL.uniqueid#</CFOUTPUT>
	   <!--- query or code to delete record --->
	</CFCASE>
	<CFCASE value=&quot;edit&quot;>
       Editing record <CFOUTPUT>#URL.uniqueid#</CFOUTPUT>
	   <!--- query or code to edit record --->
	</CFCASE>
	</CFSWITCH>

</CFIF>


<CFSET thisPage = GetFileFromPath(GetBaseTemplatePath())>
<table border=&quot;1&quot;>
<CFOUTPUT>

<TR>
<TD>Record 1</TD>
<TD><A HREF=&quot;#thisPage#?method=delete&uniqueid=1&quot;>Delete</A></TD>
<TD><A HREF=&quot;#thisPage#?method=edit&uniqueid=1&quot;>Edit</A></TD>
</TR>

<TR>
<TD>Record 2</TD>
<TD><A HREF=&quot;#thisPage#?method=delete&uniqueid=2&quot;>Delete</A></TD>
<TD><A HREF=&quot;#thisPage#?method=edit&uniqueid=2&quot;>Edit</A></TD>
</TR>

<TR>
<TD>Record 3</TD>
<TD><A HREF=&quot;#thisPage#?method=delete&uniqueid=3&quot;>Delete</A></TD>
<TD><A HREF=&quot;#thisPage#?method=edit&uniqueid=3&quot;>Edit</A></TD>
</TR>

</CFOUTPUT>
</table>

<CFOUTPUT>
<form action=&quot;#thisPage#&quot; method=&quot;post&quot;>
<INPUT TYPE=&quot;submit&quot; NAME=&quot;submit&quot; VALUE=&quot;Add&quot; />
</form>
</CFOUTPUT>

I suppose it really is six of one, half-a-dozen of another.

-Carl
 
> i.e. no action pages. the one pages does it all!

A form will always have an action page* ... but there's nothing that says it can't be itself. I do this all the time (submit to itself). And I almost always use
Code:
GetFileFromPath(GetBaseTemplatePath())
just in case, at some point, the page gets renamed or moved to a different directory, I don't have to worry about anything.



* While simply coding
Code:
<FORM METHOD=&quot;POST&quot;>
(no action page specified) will, in most cases, by default, cause the form to submit to itself, it's not valid HTML and should not be relied upon

-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top