I am creating a form with cfloop and need to access each variable individually when submitting the form. I need to use the selected entries of the form in a loop that will add my selections to a database.
This is my form:
<form method="post">
<input type="hidden" name="isPost" value="1">
...
<cfoutput>
<cfloop query="client_admin_surveys">
<input type="text" size="35" name="surveyID" id="surveyID" value="#id#">
<input type="text" size="35" name="surveyName" id="surveyName" value="#name#">
<input type="checkbox" name="amplify" id="amplify">
<input type="checkbox" name="enchance" id="enchance">
<input type="checkbox" name="pacify" id="pacify">
<input type="checkbox" name="pacifyUrgent" id="pacifyUrgent">
</cfloop>
</cfoutput>
...
<input type="submit" name="submit" value="Submit">
</form>
After posting the form, the results group all of my selections because I have the same "name" for my form elements. I tried adding an i count next to each name to make it different but then I got a bit confused about how to process the fields.
You started down the correct path when you added the counter - go back and add that, something like:
<input type="checkbox" name="amplify#client_admin_surveys.currentRow#" id="amplify">
Would work.
I also sometimes like to add a form field for the 'counter' on the processing page
<input type="hidden" name="counter" value="#client_admin_surveys.recordCount#" />
Then on the processing page, you can loop over the counter and access the form fields using bracket notation
<cfloop from="1" to="#form.counter#" indexd="i">
<cfset thisAmplify = form["amplify" & i] />
<cfset thisEnhance = form["enhance" & i] />
<!---- more logic here --->
</cfloop>
Related
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.
For example:
How many item you want to select? 1 2 3 4
If 3 is selected then
loop from 1 to 3
do something
end loop
I want everything process in the same page. Can someone let me know what I need to do? I tried cfselect and radio buttons but no luck. Thank you.
I think you're over thinking the problem. The form will return the value of the selected radio button.
HTML:
<form method="post" action="">
<p>HOW MANY YOU WANT?!? YOU CHOOSE NOW!</p>
<input type="radio" name="varname" value="1" onclick="this.form.submit();">1
<input type="radio" name="varname" value="2" onclick="this.form.submit();">2
<input type="radio" name="varname" value="3" onclick="this.form.submit();">3
<input type="submit">
</form>
ColdFusion:
<cfif isDefined("form.varname") AND form.varname GT 0>
<cfloop index="i" from="1" to="#form.varname#" step="1">
<!--- Do Stuff --->
</cfloop>
</cfif>
How can I get the checked values from some checkboxes using cfloop in coldfusion?
The checkboxes are created dynamically from a database query. ie:
<cfloop query="GetDataMaterial">
<input type="checkbox" name="MaterialID" value="#MaterialID#" />
</cfloop>
The form field will contain a comma-separated list of all values that are checked with the same form name.
For instance:
<input type="checkbox" name="MaterialID" value="1">
<input type="checkbox" name="MaterialID" value="2">
<input type="checkbox" name="MaterialID" value="4">
<input type="checkbox" name="MaterialID" value="8">
<input type="checkbox" name="MaterialID" value="16">
<input type="checkbox" name="MaterialID" value="32">
If the user Checks all of them, you'll get, assuming your form does a post:
form.MaterialID: "1,2,4,8,16,32"
If the user checks the first and last, you'll get
form.MaterialID: "1,32"
So, if you want to loop over them, you can
<cfloop list="#form.MaterialId#">
...
</cfloop>
Don't forget to have index="i" and use it to loop through the list of checkbox
<cfloop list="#form.MaterialId#" index="i">
<cfoutput>#i#</cfoutput>
</cfloop>
I have an edit object view that contains a formset(one or many if this matters), now I want to create a page that can display multiple edit object forms and submit it in a single form.
What is the correct way to achieve this task?
I found a solution.
I can enumerate my objects on edit page and use different prefixes for formsets based on these indexes. Here is an example:
First, you need enumeration, I achieved it using same input(checkbox) name with incremental values:
<input type="checkbox" name="counter" value="0">
...
<input type="checkbox" name="counter" value="1">
...
Counter numbers is the formset and other data serial numbers:
<!--Ordinary inputs-->
<input type="text" name="data0" value="value0">
<input type="text" name="data1" value="value1">
<!--Formsets-->
<input type="text" id="test0-0-data" name="test0-0-data" value="something">
<input type="text" id="test0-1-data" name="test0-1-data" value="something">
<input type="hidden" name="test0-TOTAL_FORMS" id="id_test0-TOTAL_FORMS" value="2">
<input type="hidden" name="test0-INITIAL_FORMS" id="id_test0-INITIAL_FORMS" value="0">
<input type="text" id="test1-0-data" name="test1-0-data" value="something">
<input type="hidden" name="test1-TOTAL_FORMS" id="id_test1-TOTAL_FORMS" value="1">
<input type="hidden" name="test1-INITIAL_FORMS" id="id_test1-INITIAL_FORMS" value="0">
Then if code you populate formsets like this:
counter = request.POST.getlist('counter')
for i in counter:
TestFormset = modelformset_factory(Test, form=TestForm)
test_formset = TestFormset(request.POST, prefix='test'+i, queryset=Test.objects.none())
I achieved HTML structure above with JavaScript.
I have a form in ColdFusion that initially has 5 input fields for file uploading. Should the user realize they have more than 5 files to upload in the process of attaching them, I would like the form to preserve the values when it submits itself for the change in # of fields.
Using the <cfform> tag with the preservedata="yes" attribute is supposed to accomplish this - but all I'm getting is a temp value stored in the input's value on resubmit that isn't displayed in the field nor works for a submission.
edit: Thanks for the great answers everyone, you all helped and were correct. I was able to implement Adam's suggested solution. Works great! Thanks!
function changeFieldCount() // javascript function for submit on input count change
{
var count = document.forms[0].numtotal.options[document.forms[0].numtotal.selectedIndex].value;
document.forms[0].action = "me.cfm?count=" + count;
document.forms[0].submit();
}
<cfparam name="url.count" default="5">
<cfform name="dispfiles" method="post" enctype="multipart/form-data" preservedata="yes">
<!--- looping for file input fields --->
<cfloop index="counter" from="1" to="#url.count#">
<cfinput type="file" name="file#counter#" size="50" maxlength="60"><br>
</cfloop>
<br>Number of Files to Attach:
<!--- looping for # of input selection--->
<cfselect name="numtotal">
<cfloop index="cnt" from="1" to="20" step="1">
<cfoutput>
<option value="#cnt#" <cfif #cnt# eq #url.count#>selected</cfif>>
#cnt#
</option>
</cfoutput>
</cfloop>
</cfselect>
<cfinput type="button" name="op-display" value="Display" onclick="changeFieldCount()">
<cfinput type="button" name="op-upload" value="Attach Files" onclick="submitfrm(this.form)">
<cfinput type="button" name="cancel" value=" Cancel " onclick="window.close()">
</cfform>
This is what I'm getting when I view source on the resulting submission:
<input name="file1" id="file1" type="file" value=".../cfusion.war/neotmp8858718543274653167.tmp" maxlength="60" size="50" /><br>
This is by design in all browsers for security reasons. There is no way you can insert the value for a file field.
To elaborate on #SpliFF's answer, what you need to do is dynamically create more file fields with JavaScript.
Here's an example that uses jQuery to make the JavaScript a little easier. I've also used a variable to track the number of file fields displayed so that they all have a unique number appended to their names, making it possible to loop over and uniquely identify them server-side.
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//track the current number of file fields displayed
var numUploadFields = 5;
//attach an anonymous function to the button to add more fields
$(function(){
$("#add5").click(function(){
for (var i = 0; i < 5; i++){
numUploadFields += 1;
var newHTML = "<br/><input type='file' name='upload" +
numUploadFields + "' />";
$("#someSection").append(newHTML);
}
});
});
</script>
<form method="post" action="">
<div id="someSection">
<input type="file" name="upload1" /><br/>
<input type="file" name="upload2" /><br/>
<input type="file" name="upload3" /><br/>
<input type="file" name="upload4" /><br/>
<input type="file" name="upload5" />
</div>
<input type="button" id="add5" value="Add 5 more file fields" />
</form>
Build additional file inputs using JS DOM methods. Since you aren't leaving the page this is fast and nothing is lost.