Keeping a URL variable value through pages reloads

Search script needs to keep the variables value

Page 1 of 1

4 Replies - 3675 Views - Last Post: 27 February 2009 - 10:16 AM Rate Topic: -----

#1 jgonzalez14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-November 08

Keeping a URL variable value through pages reloads

Post icon  Posted 22 February 2009 - 03:03 PM

Hi,

I am trying to make a search page for my website. I have run into a problem when i created pagination for the output.
I want to only show 10 records at a time. I am passing a form variable to the sql statement to search the database for that value. Now when you click next or previous i am setting a URL variable with the value of that form variable. This works for the first time but when you click next again the URL variable is " ". How can i fix this? I have attached the code for anyone to see the problem
<cfset request.logout = "True">
<cfparam name="StartRow" 		  default="1">
<cfparam name="DisplayRows" 	  default="10">
<cfparam name="URL.choice" 		  default="">
<cfparam name="URL.ID"			  default="">
<cfparam name="form.action"		  default="">
<cfparam name="form.search"		  default="">
<cfparam name="form.go"			  default="">
<cfparam name="URL.message"		  default="">
<cfparam name="URL.search"		  default="">
<cfparam name="form.searchData"	  default="">
<Cfparam name="variables.message" default="#URL.message#">
<cfinclude template="/resources/includes/top.cfm">
<cfinclude template="/resources/includes/header.cfm">
<cfinclude template="/Insured-Checks/resources/includes/adminNavigation.cfm">	
<h2 class="align-center">Search</h2>
<cfoutput>
		<form action="#cgi.SCRIPT_NAME#" name="search" method="post">
			<input type="hidden" name="search" value="go" />
			<table width="500">
				<tr>
					<th colspan="2">Search for data to edit and or delete</th>
				</tr>
				<tr class="row-b">
					<td class="align-center"><input class="field" type="text" name="searchdata" value="#form.Searchdata#" /></td>
				</tr>
				<tr class="row-a">
					<td class="align-center"><input type="submit" /></td>
				</tr>
			</table>
		</form>
	<script language="Javascript" type="text/javascript">
		 var frmvalidator = new Validator("search");
		 frmvalidator.addValidation("searchdata","req","Please enter your Search");
	</script>
</cfoutput>
	 
	
			<!---Start Search Engine--->
			<cfquery name="outputData" datasource="#request.ds#">
				SELECT *
				FROM Insured_Checks_AdminInfo
					where city 	 LIKE <cfif len(URL.search)> '%#trim(URL.search)#%'<cfelse> '%#trim(form.Searchdata)#%'</cfif>
					OR firstName LIKE <cfif len(URL.search)> '%#trim(URL.search)#%'<cfelse> '%#trim(form.Searchdata)#%'</cfif>
					OR lastName  LIKE <cfif len(URL.search)> '%#trim(URL.search)#%'<cfelse> '%#trim(form.Searchdata)#%'</cfif>
					OR state	 LIKE <cfif len(URL.search)> '%#trim(URL.search)#%'<cfelse> '%#trim(form.Searchdata)#%'</cfif>
					OR policyNum LIKE <cfif len(URL.search)> '%#trim(URL.search)#%'<cfelse> '%#trim(form.Searchdata)#%'</cfif>
					OR DL		LIKE <cfif len(URL.search)> '%#trim(URL.search)#%'<cfelse> '%#trim(form.Searchdata)#%'</cfif>
					OR company   LIKE <cfif len(URL.search)> '%#trim(URL.search)#%'<cfelse> '%#trim(form.Searchdata)#%'</cfif>
				ORDER BY LastName DESC
			</cfquery>
			<!---End Search Engine--->
			<Cfif outputData.recordcount gt 0>
				<h2 class="align-center">Your search returned <cfoutput>#outputData.recordcount#</cfoutput></h2>
				<cfset ToRow = StartRow + (DisplayRows - 1)>
				<cfif ToRow gt outputData.RecordCount>
					<cfset ToRow = outputData.RecordCount>
				</cfif>
				<p class="align-right">
				<cfoutput>		
					<cfset Next = StartRow + DisplayRows>
					<Cfset Previous = StartRow - DisplayRows>
					<cfif Previous GTE 1>
						<a href="#CGI.SCRIPT_NAME#?StartRow=#Previous#&search=#Form.searchdata#"><strong>Previous #DisplayRows# </strong></a>
					<cfelse>
						Previous 
					</cfif>
					<strong>|</strong>
					<Cfif Next LTE outputData.RecordCount>
						<a href="#cgi.SCRIPT_NAME#?StartRow=#Next#&search=#Form.searchdata#"><strong>Next
						<cfif (outputData.RecordCount - Next) LT DisplayRows>
							#Evaluate((outputData.RecordCOunt - Next)+1)#
						<cfelse>
							#DisplayRows#
						</cfif></strong></a>
					<cfelse>
						Next 
					</Cfif>
				</cfoutput>
			<cfelse>
				<Cfif form.searchdata neq ""><h2 class="align-center">No Records were found</h2></Cfif>
			</Cfif>
			<cfset L = 0>
			<cfoutput query="outputData" startrow="#StartRow#" maxrows="#DisplayRows#">
				<cfset L = L + 1>
				<table width="500">
					<tr>
						<th colspan="3">
							<span class="align-left">Record #L#</span> 
						</th>
					</tr>
					<tr class="row-b">
						<td colspan="2"><strong>Name</strong></td><td><strong>Policy Number</strong></td>
					</tr>
					<tr class="row-a">
						<td colspan="2">#outputData.FirstName#</td><td>#outputData.PolicyNum#</td>
					</tr>
					<tr class="row-b">
						<td><strong>Address</strong></td><td><strong>City</strong></td><td><strong>State</strong></td>
					</tr>
					<tr class="row-a">
						<td>#outputData.address#</td><td>#outputData.city#</td><td>#outputData.State#</td>
			
					</tr>
					<tr class="row-b">
						<td><strong>Driver's License</strong></td><td><strong>Company</strong></td><td colspan="2"><strong>Date Submitted</strong></td>
					</tr>
					<tr class="row-a">
						<td>#outputData.DL#</td><td>#outputData.Company#</td><td colspan="2">#outputData.Date#</td>
					</tr>
				</table>
			</cfoutput>		
			<Cfif outputData.recordcount gt 0>		
				<p class="align-right">
					<cfoutput>		
						<cfset Next = StartRow + DisplayRows>
						<Cfset Previous = StartRow - DisplayRows>
						<cfif Previous GTE 1>
							<a href="#CGI.SCRIPT_NAME#?StartRow=#Previous#"><strong>Previous #DisplayRows# </strong></a>
						<cfelse>
							Previous 
						</cfif>
						<strong>|</strong>
						<Cfif Next LTE outputData.RecordCount>
							<a href="#cgi.SCRIPT_NAME#?StartRow=#Next#"><strong>Next
							<cfif (outputData.RecordCount - Next) LT DisplayRows>
								#Evaluate((outputData.RecordCOunt - Next)+1)#
							<cfelse>
								#DisplayRows#
							</cfif></strong></a>
						<cfelse>
							Next 
						</Cfif>
					</cfoutput>
				</p>
			</cfif>	
<cfinclude template="/resources/includes/sidebar.cfm">
<cfinclude template="/resources/includes/footer.cfm">



Is This A Good Question/Topic? 0
  • +

Replies To: Keeping a URL variable value through pages reloads

#2 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Keeping a URL variable value through pages reloads

Posted 23 February 2009 - 10:44 AM

It is due to the mix of FORM and URL variables. FORM variables only exist when you submit a form using method="post". As soon as the user clicks the Previous/Next links, the FORM fields no longer exist. That is because they are using a hyperlink, not submitting a form. Since the FORM fields do not exist, they are set to the defaults you defined:

<cfparam name="form.search"		  default="">
...



The simplest fix this is to use <form method="get" ...> (typical for search forms). Then all values will be passed in the URL scope and the page should work as expected. Just remember to change all of the scopes from FORM to URL in the code.

<!--- change FORM scope --->
<cfparam name="form.search"		  default="">
<cfparam name="form.searchData"	  default="">
... etc ...

<!--- to URL ... --->
<cfparam name="url.search"		  default="">
<cfparam name="url.searchData"	  default="">
... etc ...


Was This Post Helpful? 0
  • +
  • -

#3 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Keeping a URL variable value through pages reloads

Posted 23 February 2009 - 10:58 AM

Quote

<a href="#CGI.SCRIPT_NAME#?StartRow=#Previous#">


Also, make sure you append the "search" value to all urls. You may have forgotten a few links. It is also advisable to use the URLEncodedFormat function around the #url.search# value to ensure the value is passed correctly.


Quote

Driver's License


This has nothing to do with your error. But anytime you start handling personal information like this, be sure your application has the appropriate level of security, to avoid compromises or leaks of personal information. A good rule when designing an application is "How would I feel about the level security if this application were storing my own information?" ;-)
Was This Post Helpful? 0
  • +
  • -

#4 DrWho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-February 09

Re: Keeping a URL variable value through pages reloads

Posted 26 February 2009 - 06:24 PM

why not just pass the exisiting url variable that are selcted in you form..
<form action="#cgi.SCRIPT_NAME#?#cgi.QUERY_STRING#" name="search" method="post">

#cgi.QUERY_STRING#
will grab the url vars you currently have on the page and the pass them along with your form.

OR you could just cfparam the variable without declaring the scope, form/url and if its there and you dont call the scope you would also be fine.

unless you set the variable with url or form, then the last one set would take presidence..

-paul
Was This Post Helpful? 0
  • +
  • -

#5 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Keeping a URL variable value through pages reloads

Posted 27 February 2009 - 10:16 AM

View PostDrWho, on 26 Feb, 2009 - 05:24 PM, said:

why not just pass the exisiting url variable that are selcted in you form..
<form action="#cgi.SCRIPT_NAME#?#cgi.QUERY_STRING#" name="search" method="post">


Yes, but I think the main problem is with the pagination hyperlinks. Nothing is being passed for the "search" value in the url. So when you click a link, the filter disappears.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1