Skip to main content
Inspiring
August 6, 2007
Question

POST vs GET

  • August 6, 2007
  • 8 replies
  • 1248 views
I have the search form with two fields: last name and first name. If I use method="POST" then it worked correctly which mean if I enter smith on last name field then I get all the names where last name like ‘smith%’.
BUT if I use method="GET" I don’t get where last name like ‘smith%’, I get just a select statement only no where condition for this case. Thus, in the search form, do I need to use POST or GET method?

thanks
This topic has been closed for replies.

8 replies

August 7, 2007
you should be able to use form scope and a url scope by converting both of them to attributes. and you can accomplish this buy using cf_formURL2Attributes tag which use for Fusebox application. http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&loc=en_us&extid=1001246
Inspiring
August 7, 2007
> I click on the next navigation link to go to the next page but on
> the next page, I lose the search parameters such as lastname = smith

Ignoring the other issues for a moment, lets assume you're using method="GET". Your problem is this code. When you click "next" the url.lastName/url.firstName variables no longer exist. So the CFPARAM statements kick in. So all you're doing below is setting both firstName/lastName equal to "".


<cfparam name="url.lastname" default="">
<cfparam name="url.firstname" default="">

<cfset url.lastname = lastname>
<cfset url.firstname = firstname>

<cfset Session.lastname = url.lastname>
<cfset Session.firstname = url.firstname>

<cfset url.lastname = Session.lastname>
<cfset url.firstname = Session.firstname>
...

In other words, the code above is roughly equivalent to doing this. That's why the searches return all records instead.

<cfset url.firstName = "" >
<cfset url.lastName = "" >
<cfset session.firstName = "" >
<cfset session.lastName = "" >

Inspiring
August 7, 2007
"You will never have both a form scope and a url scope."

Well you can, if you want, have both. See example.

<form method="post" action="myCFM.cfm?aField="aValue">
<input type="submit" name="bField" value="bValue">
</form>

----------

<cfdump var="#url#">
<cfdump var="#form#">
Inspiring
August 7, 2007
Here is your problem.
<cfif IsDefined("form.Submit")>
<cfset url.lastname = lastname>
<cfset url.firstname = firstname>

You will never have both a form scope and a url scope. Details are in the other answers.
kt03Author
Inspiring
August 7, 2007
Dan Bracuk ,

Then what should I do to fix my problem to make the nagivation and sort working?

thanks
Participant
August 6, 2007
Dump the url scope and see what's being passed from the form.

<cfdump var="#url#">
kt03Author
Inspiring
August 6, 2007
I got that park working but then when It didn't keep the value for the navigation and sort order. Here is my code. For example, when I enter smith for lastname, I got 50 records. I click on the next navigation link to go to the next page but on the next page, I lose the search parameters such as lastname = smith, It gave me the entired records from the database instead of records where lastname = smith. It did the same for my sort oder when I click on image for sort by ascending or decending.
Inspiring
August 6, 2007
Try scoping and trimming the values

<cfif len(trim(url.lastname)) gt 0>
AND lastname like '#url.lastname#%'
</cfif>
kt03Author
Inspiring
August 6, 2007
Already try that but it didn't work either.
Inspiring
August 6, 2007
When you use method="POST" then the form fields are accessible in the FORM scope: ie. # FORM.LastName#. When you use method="GET" the fields will be in the URL scope: ie # URL.LastName. So if you're using method="GET" use the URL scope prefix for your variables instead of FORM.

kt03Author
Inspiring
August 6, 2007
cf_dev2,

I tried your but it didnt' work. It returns all record even I enter smith for lastname

here is my code:

<form action="action.cfm" method="get" name="searchForm">
</form

action.cfm

select .....
<cfif lastname neq ""> AND lastname like '#url.lastname#%' </cfif>
<cfif firstname neq ""> AND firstname like '#url.firstname#%' </cfif>
Participant
August 6, 2007
Are you scoping the variable correctly? It's been a while since I've done any forms outside of Fusebox which automatically scopes passed variables to "attributes", but I believe a POST is going to pass the values under the "form" scope while GET is going to be "url".

-d