I am trying to use a query result from a dynamic search in my webpage by accessing it with another button that will export the result to a comma delimited file. However, the export button won’t recognize that the query exists for some reason. I keep getting an error that says: The variable QUERY is undefined.
What I am trying to do is make it so that when the user hits the export button it will save the search in a comma delimited file. Right now I am using a function that will save the search in a variable called strOutput in comma delimited form. Then I was going to write this to a file.
Since it wouldn’t recognize the query I tried setting the variable inside the search function and using a cfparam tag at the top of the page so I could access it inside the export function. This did not work either. Maybe there is a better way to do this?
Here is what the code looks like:
This is the beginning of my search function:
<cfif structKeyExists(Form, "submit")>
<cfquery datasource="#Session.HousingDataSource#" name="query">
SELECT *
This code is placed after the above code that runs a query and displays the results.
<cfif structKeyExists(Form,"export")>
<cfset strOutput = QueryToCSV(
query,"studentFirst,studentLast,studentNumber,Detail1,itemDate") />
<!---cffile
action="WRITE"
file="#filename#"
output="#strOutput#"
/--->
</cfif>
The query would need to be present in both cases as the query will not be remembered from one request to another… In the following example the query may not have been created when the second request is made.
If you update the code as follows then when you export it will run the query again to export it…
Or you can save it in the session…
Or the application scope.