Im trying to create a button that when clicked inserts a record into the database and then downloads a file. I'm trying to do this via Ajax, the file download works but it isn't inserting the record in the db.
Trigger
<button type="button" class="btn btn-primary" onClick="downloadFile1()">Download</button>
Script
function downloadFile1(){
var file1 = 'https://example.com/files/myfile.zip';
$.ajax({
TYPE: "POST",
URL: "components.cfc?method=DownloadFile1",
data: {fileId : 3},
success: function() {
window.location = file1;
}
});
}
</script>
Components.cfc
<cfcomponent output="false">
<cffunction name="DownloadFile1" access="remote" returntype="string">
<cfcontent type="text/html">
<CFQUERY NAME="i">
INSERT into t_terms
(bid, fildid)
VALUES
(
<cfqueryparam value="#session.busid#" cfsqltype="CF_SQL_INTEGER">,
<cfqueryparam value="#url.fileID#" cfsqltype="CF_SQL_INTEGER">)
</CFQUERY>
</cffunction>
Could someone point out where I am going wrong?
Like Jack said above, since you're doing a POST and not a GET, instead of looking in the URL scope, try looking in the "FORM" scope.
<cfqueryparam value="#form.fileID#" cfsqltype="CF_SQL_INTEGER">
Or, you could just look for "fileID". If it is being passed in, your query will work. If not, it will throw an error.
<cfqueryparam value="#fileID#" cfsqltype="CF_SQL_INTEGER">