ColdFusion - Form variables empty after form submit - forms

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?

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.

Related

Pass/give something other than a form to the POST request

I'm doing a form where when you select an option (with the and tag), a text below the form change according to the choice.
I would like to have this text along with my form data when send to a POST request.
I'm using Express and EJS.
Btw I also have GET parameters and would like the same thing as the text, any thoughts ?
Can you help me please ?
Thanks !
Whatever code you have that changes the text according to your choice, can also set a hidden form value in your form to the same value. That hidden form value will not display to the end user in the browser, but will be sent with the form as part of the POST (as another value of the form).
Here's an example of a hidden form element from that previous linked reference:
<input type="hidden" id="custId" name="custId" value="3487">
If this is inside your <form>, you can then change it with your Javascript to whatever you want it to and it will be automatically sent to your server as one of your form values when the form is POSTed to your server, but won't be shown to the user because of the type="hidden".

Form Variables are not showing up after form submit. ColdFusion

<form name="abc" id="abc" method="post" action="/test.cfm" enctype="multipart/form-data">
<input type="submit" name="btnSubmit" id="btnSubmit" value="OK" />
</form>
for some reason when I hit submit the "btnSubmit" is not showing up in the cfdump.
<cfdump var="#form#">
There aren't a lot of things that could cause it to simply not appear in your form. My short-list of culprits are:
Looking at the wrong file / server.
The page is being redirected via cflocation or otherwise (javascript location.replace() or location.href=x) -- this would cause that problem even if the redirection is returning the browser to the same page.
Form variables being stripped out somewhere further up, I would guess in onRequestStart
A local variable named "variables.form" was created and set to a structure further up - not very likely, but I suppose it's possible someone could accidentally write something like <cfset form = url />, which might cause that
Usually, when something like this has happened to me in the past, I've found that I've been viewing the wrong file in the browser. Usually it's come down to me looking at the same file on the wrong domain name, e.g. looking at the production server instead of the development server.
If you combine your code segments above into a single file like this (test.cfm):
<cfdump var="#form#" />
<form name="abc" id="abc" method="post" action="test.cfm" enctype="multipart/form-data">
<input type="submit" name="btnSubmit" id="btnSubmit" value="OK" />
</form>
That ought to give you some insight into your problem. Note that I removed the leading-slash / in the form action, so that this form will post to itself. When I first view this template, I see an empty struct (followed by the button), because I haven't put anything in the form scope yet. When I submit the form I then see two elements in the structure, fieldnames and btnSubmit. That's another good indicator, if you don't see fieldnames in the form structure, then your CFML page may not have received a form submission. If you know you're looking at the right page and you've submitted the form and you still don't have the fieldnames entry, then I'd start looking for potential browser redirection.
You might also want to add an empty Application.cfc in the same directory just to be sure that there's not an application interfering with it. It's possible that something in the onRequestStart might be stripping out form variables with the name "btnSubmit" or even any form variable with the string "submit" anywhere in the name. I wouldn't expect it though -- I'd look for other causes like cflocation tags first.

Form Entry, Review then Update

I'm creating a form where the user will enter data, then click "Review" to see the data they entered. Then after review, "Save" the record.
I'm using a <cfform> to submit the form but wonder how to submit the data to the database from the "Review" page since there's no form here.
Should I set variables like FORM.Name = VARIABLES.Name to display on the review page, then convert them back after they submit the "Save"?
Not really sure what to do here. Overthinking?
Just keep in form scope. Basic output of form values, using baked in form.fieldList:
<cfloop list="#form.fieldList#" item="fieldName">
<cfoutput>
#fieldName#: #form[fieldName]#<br>
<input type="hidden" name="#fieldName#" value="#form[fieldName]#">
</cfoutput>
</cfloop>
Link to original form or submit to page where you save it, using values from hidden fields (which should mirror original form values)
A few other ways you could do this, but this seems simplest to me.
Why not using a second form with hidden input fields for the review page? Confirm would be a submit button to the page that saves the data to the server.
Second possibility (not that proper): save the data directly into the database and load it for the review from there. Trick: use a flag with "confirmed" and set it if confirmed. Bad part about that: you have to clean up older data that has not been confirmed.
Improving on Billy Cravens answer. This protects against the fields having Embedded Attacks
<cfoutput>
#lcase(fieldName)#: #xmlFormat(form[fieldName])#<br>
<input type="hidden" name="#fieldName#" value="#xmlFormat(form[fieldName])#" />
</cfoutput>
Also this will not work for image uploads. The lcase(fieldName) is to keeps the fields from being displayed as all caps. Lowercase is often easier to read.

Paypal IPN Custom field

I know I can send a custom field using IPN with $_POST['custom']
But can I do that with an uploaded file? More specifically an uploaded image?
And what if I have two custom fields? I previously used something like this:
<input type="hidden" name="custom" value="<?php echo $a.'|'.$b ?>"/>
But that was just text! Now I want to upload a file and I also have custom text, then I want to get it.
Is this possible and how would that look like?
Thanks!
I don't think it's possible to do it the way you're describing, but here's an alternative that I have used in the past.
Instead of having the form that contains the file upload post to PayPal, have it post to your site, and then store that uploaded file and any other custom data in a database (or any other way you choose to store it). Assign that data an id.
Now redirect the user to a page that contains basically the same form, except that the input fields should be hidden, and the form will post to PayPal. Fill in that form programmatically with the data from the previous post, and fill the 'custom' field with the id that you assigned to the custom data. This page would also contain a JavaScript statement like this (at the bottom after the form, to ensure that it doesn't execute until the form is loaded)...
<script type="text/javascript">
document.forms["paypalform"].submit();
</script>
...to automatically submit the form when the page is loaded. It's still a good idea to leave a submit button (you could style it as a link, if you want) in case the user has JavaScript disabled. It could say something like "Click here if you are not redirected to PayPal within 10 seconds." You could also add another message on the page such as "Redirecting to PayPal."
Now when you get your PDT or IPN information back from PayPal for that transaction, the 'custom' field will contain the id you assigned to the data earlier. It's just a matter of retrieving the data from wherever you stored it.
I've done this in ASP.NET before, and I assume it would work just as well in PHP (the server-side parts), but I can't say for sure.
Note: The 'custom' field can only contain up to 256 characters.

Joomla module submit form can't access database

I've been searching the net for an answer to my question but I just can't seem to find one, even though it's probably pretty simple.
I have a joomla module that signs up users to a newsletter, when clicking the submit button I navigate to submitsignup.php file. I do this using the form action value like so:
form action="modules/mod_cmsnewslettersignup/otherfiles/submitsignup.php" method="post" id="subForm"
Within this submitsignup.php file I can not access any joomla classes, such as:
$db = JFactory::getDBO();
I know that I can't access any joomla classes because I made direct access to the submitsignup.php file, but I was wondering how do I access this file so that I can have access to all the Joomla classes?
Thanks.
If you are reloading the page when you submit the form, then there is a simple solution that solves both the problem of using a direct URL and of having to load the Joomla framework in that file. Basically change your module code to something like this -
if ($_POST["formsubmitted"]){
the code you run when the form is submitted
echo success or failure message
} else {
the code you run to display the form
<form action="<?php echo JURI::current(); ?>" method="post">
<input type="hidden" value="true" name="formsubmitted">
}
Basically, you submit the form to the page that displays it. Then in your module you add a hook to either process the submitted form or display the form depending on what you find in $_POST. You can add some simple security to make sure that the form is being submitted from your site.