Connecting Tech Pros Worldwide Help | Site Map

Have to submit form twice to get URL query to appear correctly

Newbie
 
Join Date: Nov 2006
Posts: 23
#1: Jan 14 '07
I'm submitting a form to the same page. Form variables are being output into the URL and the query works from the URL variables. Problem is, I have submit the form twice before the form field I have selected are passed to the URL variables. In between, it goes back to the default form and url variables that I set to zero. Any idea what going wrong? Interestingly, if I change Post to Get, it works the first time. Here's the code:

Expand|Select|Wrap|Line Numbers
  1.  <cfparam name="PageNum_display" default="1"> 
  2. <cfparam name="URL.grade" default="0">
  3. <cfparam name="URL.subject" default="0">
  4. <cfparam name="FORM.grade" default="0">
  5. <cfparam name="FORM.subject" default="0">
  6. <cfquery name="display" datasource="abc">
  7. SELECT *
  8. FROM ActivityTable
  9. WHERE grade =
  10. <cfqueryparam value="#URL.grade#" cfsqltype="cf_sql_numeric"> AND subject =
  11. <cfqueryparam value="#URL.subject#" cfsqltype="CF_SQL_VARCHAR">
  12. </cfquery>
  13.  
Here's the form:
[HTML]
<form name="form1" method="post" action="activitydisplay.cfm?grade=<cfoutput>#Form. grade#</cfoutput>&subject=<cfoutput>#Form.subject#</cfoutput>">
[/HTML] I know the code isn't perfect so please be kind.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jan 14 '07

re: Have to submit form twice to get URL query to appear correctly


Quote:

Originally Posted by windsorben

Any idea what going wrong? Interestingly, if I change Post to Get, it works the first time. Here's the code:

<cfparam name="PageNum_display" default="1">
<cfparam name="URL.grade" default="0">
<cfparam name="URL.subject" default="0">
<cfparam name="FORM.grade" default="0">
<cfparam name="FORM.subject" default="0">
<cfquery name="display" datasource="abc">
SELECT *
FROM ActivityTable
WHERE grade =
<cfqueryparam value="#URL.grade#" cfsqltype="cf_sql_numeric"> AND subject =
<cfqueryparam value="#URL.subject#" cfsqltype="CF_SQL_VARCHAR">
</cfquery>

Here's the form:
<form name="form1" method="post" action="activitydisplay.cfm?grade=<cfoutput>#Form. grade#</cfoutput>&subject=<cfoutput>#Form.subject#</cfoutput>">

I know the code isn't perfect so please be kind.

The url scope is for GET and the form scope is for POST.

Your form is coded incorrectly for what you require. The reason why the GET request works first time is that you are sending a GET request by passing the values in the URL.

For a POST method, try the following for your form
[HTML]<input type="text" name="grade" value="">
<input type="text" name="subject" value="">[/HTML]
This will pass your values in a POST request.

Finally, your query must use the form scope for a POST request:
[HTML]<cfquery name="display" datasource="abc">
SELECT *
FROM ActivityTable
WHERE grade =
<cfqueryparam value="#FORM.grade#" cfsqltype="cf_sql_numeric"> AND subject =
<cfqueryparam value="#FORM.subject#" cfsqltype="CF_SQL_VARCHAR">
</cfquery>
[/HTML]
Newbie
 
Join Date: Nov 2006
Posts: 23
#3: Jan 16 '07

re: Have to submit form twice to get URL query to appear correctly


Yeah, that would work but the page navigation generated by dreamweaver works with URL variables and then when I hit the next button my form variables no longer exist and then I get no second page of results (even when there are more results). I guess with the page navigation generated by dreamweaver, I'll have to convert my form varialbes to url variables and then use the GET method. I guess this isn't a big deal since no "secret" information is being passed. Thanks for your help.
Ben

Quote:

Originally Posted by acoder

The url scope is for GET and the form scope is for POST.

Your form is coded incorrectly for what you require. The reason why the GET request works first time is that you are sending a GET request by passing the values in the URL.

For a POST method, try the following for your form
[HTML]<input type="text" name="grade" value="">
<input type="text" name="subject" value="">[/HTML]
This will pass your values in a POST request.

Finally, your query must use the form scope for a POST request:
[HTML]<cfquery name="display" datasource="abc">
SELECT *
FROM ActivityTable
WHERE grade =
<cfqueryparam value="#FORM.grade#" cfsqltype="cf_sql_numeric"> AND subject =
<cfqueryparam value="#FORM.subject#" cfsqltype="CF_SQL_VARCHAR">
</cfquery>
[/HTML]

Reply