ColdFusion - Form variables empty after form submit

1.1k Views Asked by At

My question references the following question/answer provided in this stackoverflow post: Form Variables are not showing up after form submit. ColdFusion

I wanted to comment in the above referenced post, but I don't have enough reputation points. I see the answer by Samuel Dealey above indicating that using a cflocation or location.replace() could result in Form variables not showing up. That is exactly what is happening in my scenario.

I have a simple registration form submission. Upon form submission the form data is sent to a page named addCampaign.cfm. addCampaign.cfm contains code that will write the registration data to the database. After writing the registration info to database, I verify that info was written to database. At that point I then redirect the user to a specific page if registration was successful, and if not successful then redirect back to the registration form page with an appropriate error message.

I have tried using both for the redirect, and have attempted using javascript location.replace(), both result in the same problem.

The issue I am running into is that:

1) The redirect never occurs

2) I am using to display the struct, but it lists it as empty

3) I have removed the cflocation and location.replace() and can verify that my form elements do exist in the form struct.

4) The form data is being written to the database, which is very strange, considering that the form struct is being displayed as empty.

I don't understand why the form struct is empty when the data is being written to the database, and furthermore I don't know why the redirect does not work. Can anybody provide some clarification on why this would be happening?

1

There are 1 best solutions below

2
On BEST ANSWER

Consider this code on a single file

<cfif cgi.request_method EQ "post">
    <cfdump var="#form#">

    <!--- More importantly, DB inserts --->
</cfif>



<form method="post" action="?">
  <!--- Lots of other fields go here too --->

  <input type="submit" name="btnSubmit" id="btnSubmit" value="OK" />
</form>

If you do it this way, you don't have to worry about pushing data over redirect of some sort. You are already on the page you want. action="?" basically means submit to the same field as I am already on. Note that the file's behavior is different on a GET vs POST.