Coldfusion form submission multiple fields with same name in CF8 & CF11 - coldfusion

I am getting different output when dumping results of a form submission with multiple fields with the same name. In CF8 I got only first html element value in output result but in CF11 I got value as a list. Is this specific to server running in CF8 & CF11. Below is the prototype of code I am using
<cfform method="post" action="">
<select name="test">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="hidden" name="test" value="0">
<input type="submit" name="btnsubmit" />
</cfform>
<cfif structKeyExists(form,"btnsubmit")>
<cfdump var="#form#">
</cfif>
In CF8 I got results as '1' but in CF11 I got result as '1,0'.
Please let me know if this is specific to my server upgrade. I just want to know that in CF8 server multiple fields with same name returns value as list or only returns value of first element.

Related

Radio button not checked breaks page but works if checked

I have a simple form that I'm trying to pass data to which on the action page has an email that sends the data that I put into the form.
On my form page. I have a text field and two radio buttons.
My text field works if I don't put data into it. However, on my radio buttons the page works if I check it but doesnt work if I don't check it at all which results in my page breaking I'm not sure what I'm doing wrong? I want the page to process the default to "no" if I didn't check either radio buttons. Do I have to validate or something?
Here is my code.
<cfparam name="form.firstName" default="">
<cfparam name = "form.optradio1" default="no">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif form.optradio1 eq "Yes">checked</cfif>>
</form>
That's how radio and checkbox inputs work in HTML. If they are not checked, they are not submitted on the form submit.
To determine if the radio input was checked, you can use
structKeyExists(form, <name of the input, as string>) as in
structKeyExists(form, "optradio1").
<cfparam name="form.firstName" default="">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif structKeyExists(form, "optradio1")>checked</cfif>>
</form>
Assuming you have two radio inputs:
<cfparam name="form.firstName" default="">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif structKeyExists(form, "optradio1") and form.optradio1 eq "Male">checked</cfif>>
<input type="radio" name="optradio1" Value="Female" <cfif structKeyExists(form, "optradio1") and form.optradio1 eq "Female">checked</cfif>>
</form>
Your initial code doesn't work because:
if checked, form.optradio1 was equal to Male
if not checked, form.optradio1 was defaulted to no because of
<cfparam name = "form.optradio1" default="no">

Passing <cfselect> hard coded values into a session scope

I have a cfform with multiple fields and I'm using an onpage post method to post information gathered from the user's entries into a session to pass onto each page of the registration process.
What I cannot figure out for the life of me and this is probably something simple is that if I have hard coded options for a how can I pass what is selected into the session? I can get a session.house.main.form.saletype is undefined error when trying to display the value selected on the next page. All my other form fields show up fine.
I removed all the other form fields to make it easier to check my code:
<cfif not structKeyExists(session.house, "main")>
<cfset session.house.main= {saletype="",
name=""}>
</cfif>
<cfparam name="form.saletype" default="#session.house.main.saletype#">
<cfparam name="form.name" default="#session.house.main.name#">
<cfif structKeyExists(form, "submit")>
<cfset session.house.main = {saletype=form.saletype,
name=form.name}>
<cflocation url="page2.cfm" addtoken="false" />
</cfif>
<cfform method="post">
<cfselect name="saletype" size="1">
<option value="Lease" selected>Lease</option>
<option value="Rent Now">Rent Now</option>
</cfselect><br />
<cfinput type="text" name="name" id="name" value="#form.name#" required="yes" message="Please enter your name"><br />
<cfinput type="submit" name="submit" id="submit" value="Save"><br />
</cfform>
Edit: Fixed cfselect name. How would I set the form.saletype into the cfselect with two hardcoded options?

select statement displays full list but not in a select statement

I am struggling to find out where I have gone wrong, and have spent hours trying to find out why this is happening, I think its just something I have missed but I cant spot it.
What happens is that the select statement instead of showing a dropdown which i can just select a team to delete displays all of the teams in a list with a delete button next to it, yet when I use the select statement on another page it works fine. if anyone can point out where I have gone wrong that would be appreciated
Cheers
<cfquery name="deleteteam" datasource="danny2">
SELECT *
FROM pool_teams
</cfquery>
<html>
<head>
<title>LCF Delete Team</title>
</head>
<body>
<cfif IsDefined('Form.delete_button')>
<cfoutput>
<form action="#CGI.SCRIPT_NAME#" method="post">
<input type="hidden" name="ID" value="#FORM.ID#"/>
do you really want to delete record?
<input type="submit" name="confirm_button" value="Yes">
<input type="submit" name="cancel_button" value="No">
</form>
</cfoutput>
<cfelseif IsDefined('FORM.confirm_button')>
<cfquery datasource="danny2">
DELETE FROM pool_teams
WHERE ID = '#FORM.ID#'
</cfquery>
The record has been deleted
<cfoutput> Return to list</cfoutput>
<cfelseif IsDefined('FORM.cancel_button')>
<cflocation url="#CGI.SCRIPT_NAME#" >
<cfelse>
<cfoutput query="deleteteam">
<form action="#CGI.SCRIPT_NAME#" method="post">
<select>
<option value="#ID#">#teamname#</option>
</select>
<input type="hidden" name="ID" value="#deleteteam.ID#">
<input type="submit" name="delete_button" value="delete"/>
</form>
</cfoutput>
</cfif>
</body>
</html>
<cfoutput query="deleteteam">
<form action="#CGI.SCRIPT_NAME#" method="post">
<select>
<option value="#ID#">#teamname#</option>
</select>
<input type="hidden" name="ID" value="#deleteteam.ID#">
<input type="submit" name="delete_button" value="delete"/>
</form>
</cfoutput>
This is where you are going wrong. If you think of outputting a query as performing a loop. each time you loop over the query you are making another form with one select option in it.
You should change your code to look something like this.
<form action="#CGI.SCRIPT_NAME#" method="post">
<select>
<cfoutput query="deleteteam">
<option value="#deleteteam.ID#">#deleteteam.teamname#</option>
</cfoutput>
</select>
<input type="hidden" name="ID" value="#deleteteam.ID#">
<input type="submit" name="delete_button" value="delete"/>
</form>
What My code is doing is just adding options for each query item not the complete from.
Hope that makes sense.

coldfusion - how to loop the value that was passed from getElementById in the same page?

I want to loop the selected number from the list (cfselect). I tried getElementById but only able to display it on the same page. I cannot pass this number to the loop. Can someone help me? Thank you.
function item()
var a = document.formName.numList.selectedIndex;
document.getElementById('i').value = document. family.tBro.options[a].value;
var n=document. family.tBro.options[a].value;
<!----OTHER INPUT TEXT BOXES --->
<cfform name="family" action="complete.cfm" method="post">
How many brothers do you have?
<cfselect name="tBro" onChange="item();" required="yes">
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
<option value="4">4</option>
</cfselect>
<!---DISPLAY THE SELECTED CHOICE from getElementById--->
Total number of brothers: <cfinput type="text" name="i" id="i">
<!---LOOP x amount of time from selected choice above.
For example, if 2 is selected, the below info will display two times
--->
<cfinput type="text" name="firstname" required="yes">
<cfinput type="text" name="lastname" required="yes">
<cfinput type="text" name="Age" required="yes">
<cfinput type="text" name="Ocupation" required="yes">
<!--- END LOOP--->
Tip: state what you're trying to accomplish, rather than the implementation. I had to reread it a few times to understand your need, in which case what your implementation isn't really a good fit.
You're trying to pass JavaScript to CFM code: that's not the way it works. ColdFusion is rendered on the server; JavaScript is rendered on the client. At the moment when item() is called, ColdFusion has finished all of its rendering; you can't effect a CF loop with the result of item().
Without a really complex AJAX solution, you have 2 choices:
refresh the browser when init() is called where you pass dropdown value in the url (not good, as you'd lose state of other form fields)
use something else like jQuery to render your dynamic list of text fields. This is probably the best approach, and a common one. The downside of this is you'll need to implement things like "required" in jQuery, which isn't a big deal, and a common use case.

displaying questions twice

can anyone review this code tell me what's wrong in it? I don't understand why it is displaying questions two times.
here is the code to display questions based on its questiontype, I mean it will look into question folder for matching questiontype template and then display it with question.
this is the code to show survey's questions.
<cfoutput>
<cfset step = 0 />
<form class="form form-horizontal" action="#buildUrl(action='survey.savesurveyresults',querystring='surveyId=#rc.surveyid#')#" method="post">
<input type="hidden" name="id" value="0">
<input type="hidden" name="fksurveyid" value="#rc.surveyId#">
<input type="hidden" name="fkquestionid" value="#rc.questions.id#">
<fieldset>
<cfloop query="rc.questions">
<cfset step ++ />
<cfset answer = "" />
<cfmodule template="../question/#rc.questions.template#/display.cfm" step="#step#" question="#rc.questions.question#" template1="#rc.questions.template#" fkquestionid="#rc.questions.id#" answer="#answer#" required="#rc.questions.required#" result="result#step#"/>
</cfloop>
<div class="form-actions">
<button type="submit" name="submit" class="btn btn-success">Submit answers</button>
</div>
</fieldset>
</form>
</cfoutput>
this is my display.cfm to view question and its questiontype like truefalse or yes or no.
<cfparam name="attributes.yesno" default="false">
<cfoutput>
<p>#attributes.step#) #attributes.question# <cfif attributes.required EQ 1><strong>* </strong></cfif></p>
<div class="answers">
<cfif attributes.yesno>
<input type="radio" name="answer" id="answer" value="yes"<cfif attributes.answer is "yes">Checked</cfif>><label for="truefalse">Yes</label><br>
<input type="radio" name="answer" id="answer" value="no"<cfif attributes.answer is "No">Checked</cfif>><label for="truefalse">No</label>
<cfelse>
<input type="radio" name="answer" id="answer" value="true"<cfif attributes.answer is "true">Checked</cfif>><label for="truefalse">True</label><br>
<input type="radio" name="answer" id="answer" value="False"<cfif attributes.answer is "False">Checked</cfif>><label for="truefalse">False</label>
</cfif>
</div>
</cfoutput>
here is the query to list question's records.
<cfquery name="list">
SELECT
questions.id,
questions.question,
questions.rank,
questions.required,
questiontypes.name as questiontype,
questiontypes.template as template,
surveys.name as surveysname,
surveys.thankyoumsg as thankyoumsg
FROM questions
INNER JOIN questiontypes ON questions.fkquestiontypeid = questiontypes.id
INNER JOIN surveys ON questions.fksurveyid = surveys.id
WHERE questions.fksurveyid = <cfqueryparam cfsqltype="cf_sql_bigint" value="#arguments.surveyid#">
</cfquery>
This is something that has bitten me a couple of times. I've always been pretty big on closing tags. But this is a situation where it will hurt. And be hard to debug if you don't understand the behavior of cfmodule. As the post above mine states, if you close the cfmodule tag, it will execute twice. This is because it's treated the same as a custom tag. There may be situations where you want to process part of the tag when it's first run and the rest after it's complete. You can access the ExecutionMode in the thisTag scope of the cfmodule page. With no closing tag, it's simply run in the thisTag.ExecutionMode = Start mode. If you close it, it runs the tag again in the End mode. If you aren't doing anything with the ExecutionMode inside the cfmodule's code, the whole thing will simply run again. This behavior is part of the reason that cfmodule can be so powerful.
When using the <cfmodule> tag you need to remember that ColdFusion will call that tag twice if you include an ending </cfmodule> tag OR if you close the opening tag like so <cfmodule ... />.
As stated on the cfmodule documentation page:
If you specify an end tag to cfmodule, ColdFusion calls your custom tag as if it had both a start and an end tag. For more information, see Handling end tags in the Developing ColdFusion Applications.
Handling end tags in the Developing ColdFusion Applications
In order to avoid this functionality do not close your <cfmodule> tag.
I got it, i must have not to close the cfmodule tag like <cfmodule />.