Link to home
Start Free TrialLog in
Avatar of John Wilkinson
John WilkinsonFlag for United States of America

asked on

In a Coldfusion <cfif> block, how to set the value of a hidden field?

I have a hidden field set up like this on a CFM page:

<input type="hidden" id="downloadFlag" name="downloadFlag" value="0">

Open in new window


I also have a CF variable associated with it:

variables.downloadFlag = form.downloadFlag;

Open in new window


So when the CFM page initially loads , this value is equal to "0". I have a button on the form that sets it to "1" when a download has been requested.  

Then I have this code:

<cfif (variables.downloadFlag eq "1")>
    <cfscript>
        variables.d_srx = CreateObject('component','mycfcpage');
        variables.d_success  = variables.d_srx.download_files(variables.q_data);
    </cfscript>
    document.getElementById('downloadFlag').value = 0;
</cfif>

Open in new window

that executes the download function on a CFC page if the value of the flag="1". What I need to do after the function has executed is re-set the value of the flag back to 0. That's what I try to do on the line:

document.getElementById('downloadFlag').value = 0;

Open in new window


but this isn't working. I get no error, yet when I check the value with the inspector after the download, it is still "1".

Is there some CF-specific syntax I need to use to get this field value set back to "0"?





Avatar of Charlie Arehart
Charlie Arehart

That code setting that variable in the browser is Javascript, and as such needs to be wrapped in a script block (<script>), separate from the cfscript block (<cfscript>). 

This is an easy thing to trip over/lose track of. The languages look similar but are very different. Let us know if that solves the problem. 
Avatar of John Wilkinson

ASKER

I tried this:

<cfif (variables.downloadFlag eq "1")>
    <cfscript>
        variables.d_srx = CreateObject('component','Srx_Files');
        variables.d_success  = variables.d_srx.download_files(variables.q_data);
    </cfscript>
    <script>
        alert("Re-set flag...");
        document.getElementById('downloadFlag').value = 0;
    </script>
</cfif>

Open in new window

but I didn't see the alert, and the flag is still 1:




User generated image
The button is set up like this:
<button id="DownloadBtn" class="btn btn-default edit" type="submit" onclick="setDownloadFlag()">Download</button>

From what I understand, it performs the onclick event (which sets the flag value to "1"), then submits the page.
It definitely works, because the download actually happens when the button is clicked. But then the flag remains set at "1", and I want it to re-set to "0".
It may that the cfml code you run in the download_files method may stop cfml processing, such that NO code after that call is executed. Add a test alert in the <script> block. Do you see it appear? You could also add a writelog statement to the cfscript block (or a cflog tag between the two blocks) to see if it would generate output to a log.

If processing does stop, then you'd want to set your flag to 1 BEFORE the method call, and you could consider puttting a try/catch surrounding the method call which sets it to 0 in the catch (if the method call fails). 
Processing does appear to stop after the function call (in:  variables.d_success  = variables.d_srx.download_files(variables.q_data);).Nothing I put after that runs.

The flag is set to "1" prior to the function call, I know that because the download actually happens. The problem is I can't set it back to "0".



ASKER CERTIFIED SOLUTION
Avatar of John Wilkinson
John Wilkinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad you solved it. That said, I think you'd agree your first example had the problem that I solved in my first reply...and it would apply in your new variant as well (the need to distinguish js code from cfml with their own script blocks). Just offering that as much for any future readers, as this is not indicated in your "answer" (which some will read without reading all other comments).