I'm using cfloop to dynamically create/fill in three sets of input fields, plus some radio buttons that are used for a rating system. The input fields work as expected. However I'm having trouble with the radio buttons.
For some reason (and I'm assuming it's a simple reason) the radio buttons don't reflect the values being sent to them. For example: Say I have 3 things I am rating. If the looped values are 5,4,3 the radio buttons are all displaying as if the value being passed to them was 5. It's almost as if all of the field sets are created first and all take the first value (e.g. get checked), as opposed to creating the first field set, insert the value, and then create the second field set and so on (like I would assume in a loop). Again, it works for the everything but the radio buttons. Any insight would be greatly appreciated.
Here is my code:
<cfloop query="postedBy" startrow="1" endrow="4">
<cfquery name="score" datasource="myDB">
SELECT Round(sum(leadership)/Count(leadership)) as leadership
, Round(sum(communication)/Count(communication)) as communication
, Round(sum(fairness)/Count(fairness)) as fairness
, Round(sum(ethics)/Count(ethics)) as ethics
, Round(sum(competence)/Count(competence)) as competence
FROM score_base
WHERE score_ID = '#postedBy.score_id#'
</cfquery>
<cfset score_ID=#postedBy.score_id#>
<cfoutput>id: #postedBy.score_id#</cfoutput>
<cfoutput>My score: #score.leadership#, #score.communication#, #score.fairness#, #score.ethics#, #score.competence#</cfoutput>
<cfset counter = counter+1>
<cfset "currentScore#score.leadership#" = "checked">
<cfset "currentScoreb#score.communication#" = "checked">
<cfset "currentScorec#score.fairness#" = "checked">
<cfset "currentScored#score.ethics#" = "checked">
<cfset "currentScoree#score.competence#" = "checked">
...
<div class="rating-wrapper">
<cfform>
<label>Leadership</label>
<cfinput type="radio" class="star" name="leadership" value="1" disabled="disabled" checked = '#currentScore1#'/>
<cfinput type="radio" class="star" name="leadership" value="2" disabled="disabled" checked = '#currentScore2#'/>
<cfinput type="radio" class="star" name="leadership" value="3" disabled="disabled" checked = '#currentScore3#'/>
<cfinput type="radio" class="star" name="leadership" value="4" disabled="disabled" checked = '#currentScore4#'/>
<cfinput type="radio" class="star" name="leadership" value="4" disabled="disabled" checked = '#currentScore5#'/>
</cfform>
</div><!-- END div class="rating-wrapper" -->
<div class="rating-wrapper">
<cfform>
<label>Communication</label>
<cfinput type="radio" class="star" name="communication" value="1" disabled="disabled" checked = '#currentScoreb1#'/>
<cfinput type="radio" class="star" name="communication" value="2" disabled="disabled" checked = '#currentScoreb2#'/>
<cfinput type="radio" class="star" name="communication" value="3" disabled="disabled" checked = '#currentScoreb3#'/>
<cfinput type="radio" class="star" name="communication" value="4" disabled="disabled" checked = '#currentScoreb4#'/>
<cfinput type="radio" class="star" name="communication" value="5" disabled="disabled" checked = '#currentScoreb5#'/>
</cfform>
</div><!-- END div class="rating-wrapper" -->
<div class="rating-wrapper">
<cfform>
<label>Fairness</label>
<cfinput type="radio" class="star" name="fairness" value="1" disabled="disabled" checked = '#currentScorec1#'/>
<cfinput type="radio" class="star" name="fairness" value="2" disabled="disabled" checked = '#currentScorec2#'/>
<cfinput type="radio" class="star" name="fairness" value="3" disabled="disabled" checked = '#currentScorec3#'/>
<cfinput type="radio" class="star" name="fairness" value="4" disabled="disabled" checked = '#currentScorec4#'/>
<cfinput type="radio" class="star" name="fairness" value="5" disabled="disabled" checked = '#currentScorec5#'/>
</cfform>
</div><!-- END div class="rating-wrapper" -->
<div class="rating-wrapper">
<cfform>
<label>Ethics</label>
<cfinput type="radio" class="star" name="ethics" value="1" disabled="disabled" checked = '#currentScored1#'/>
<cfinput type="radio" class="star" name="ethics" value="2" disabled="disabled" checked = '#currentScored2#'/>
<cfinput type="radio" class="star" name="ethics" value="3" disabled="disabled" checked = '#currentScored3#'/>
<cfinput type="radio" class="star" name="ethics" value="4" disabled="disabled" checked = '#currentScored4#'/>
<cfinput type="radio" class="star" name="ethics" value="5" disabled="disabled" checked = '#currentScored5#'/>
</cfform>
</div><!-- END div class="rating-wrapper" -->
....
</cfloop>
Figured it out: The issue was with my dynamic variables. I needed to reset them at the end of the loop:
<cfset "currentScore#score.leadership#" = "0">
<cfset "currentScoreb#score.communication#" = "0">
<cfset "currentScorec#score.fairness#" = "0">
<cfset "currentScored#score.ethics#" = "0">
<cfset "currentScoree#score.competence#" = "0">
</cfloop>
Related
Im trying to use the code I have found and its not working properly it is always saying that I am a robot do you have any idea why this will not work?
The Application.cfc has the site and secret key in it.
<script src="https://www.google.com/recaptcha/api.js?render=<cfoutput>#application.SiteKey#</cfoutput>"></script>
<cfif ISDEFINED('FORM.FirstName')> <!--- check if form was submitted and if so run code below --->
<cfhttp url="https://www.google.com/recaptcha/api/siteverify?secret=#application.SecretKey#&response=#FORM['g-recaptcha-response']#" result="Response" />
<cfset Return = deserializeJSON(Response.FileContent) />
<cfif Return.success IS 'true' AND Return.score GT 0.0> <!--- check if true and if score is greater than 0.5. Run code below if all good. --->
<cfoutput>Human: #FORM.FirstName# #FORM.LastName#</cfoutput>
<!--- you can do database entry and/or email results here --->
<cfelse> <!--- if not a human, do this. I usually remove the else part completely, but if you need to do something with the robot, do it here. --->
Most likely a robot.
</cfif>
<cfelse> <!--- show form --->
<form method="post" action="/contact.cfm"> <!--- submit form back to itself --->
First Name: <input name="FirstName" type="text"><br>
Last Name: <input name="LastName" type="text"><br>
<input name="submit" type="submit">
<input name="g-recaptcha-response" id="g-recaptcha-response" type="hidden" /> <!--- javascript below gives this a value from google. --->
</form>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('<cfoutput>#application.SiteKey#</cfoutput>', {action: 'homepage'})
.then(function(token) {
document.getElementById('g-recaptcha-response').value=token;
});
});
</script>
</cfif>
This is how I was able to get the form working properly.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://www.google.com/recaptcha/api.js?render=YOUR SITE KEY"></script>
<!-- contact form demo container -->
<cfif ISDEFINED('FORM.name')> <!--- check if form was submitted and if so run code below --->
<cfhttp url="https://www.google.com/recaptcha/api/siteverify?secret=#application.SecretKey#&response=#FORM['token']#" result="Response" />
<cfset Return = deserializeJSON(Response.FileContent) />
<cfif Return.success IS 'true' AND Return.score GT 0.5> <!--- check if true and if score is greater than 0.5. Run code below if all good. --->
<cfelse> <!--- if not a human, do this. I usually remove the else part completely, but if you need to do something with the robot, do it here. --->
</cfif>
<cfelse>
<section style="margin: 50px 20px;">
<div style="max-width: 768px; margin: auto;">
<!-- contact form -->
<div class="card">
<h2 class="card-header">Contact Form</h2>
<div class="card-body">
<form class="contact_form" method="post" action="contact.cfm">
<!-- form fields -->
<div class="row">
<div class="col-md-6 form-group">
<input name="name" type="text" class="form-control" placeholder="Name" required>
</div>
<div class="col-md-6 form-group">
<input name="email" type="email" class="form-control" placeholder="Email" required>
</div>
<div class="col-md-6 form-group">
<input name="phone" type="text" class="form-control" placeholder="Phone" required>
</div>
<div class="col-md-6 form-group">
<input name="subject" type="text" class="form-control" placeholder="Subject" required>
</div>
<div class="col-12 form-group">
<textarea name="message" class="form-control" rows="5" placeholder="Message" required></textarea>
</div>
<!-- form message prompt -->
<div class="row">
<div class="col-12">
<div class="contact_msg" style="display: none">
<p>Your message was sent.</p>
</div>
</div>
</div>
<div class="col-12">
<input type="submit" value="Submit Form" class="btn btn-success" name="post">
</div>
<!-- hidden reCaptcha token input -->
<input type="hidden" id="token" name="token">
</div>
</form>
</div>
</div>
</div>
</section>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('YOUR SITE KEY', {action: 'homepage'}).then(function(token) {
// console.log(token);
document.getElementById("token").value = token;
});
// refresh token every minute to prevent expiration
setInterval(function(){
grecaptcha.execute('YOUR SITE KEY', {action: 'homepage'}).then(function(token) {
console.log( 'refreshed token:', token );
document.getElementById("token").value = token;
});
}, 60000);
});
</script>
</cfif>
<!-- References for the optional jQuery function to enhance end-user prompts -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
This is how I passed the values to the API. Again, just passing along code that worked, not saying this is the only way
<cfhttp method="post" url="https://www.google.com/recaptcha/api/siteverify" result="Response">
<cfhttpparam name="secret" type="formField" value="#application.SecretKey#">
<cfhttpparam name="response" type="formField" value="#form["g-recaptcha-response"]#">
</cfhttp>
Trying to send <cfmail> from a contact us static page. It will have a single recipient and I don't want to save it on the back end.
<cfcase value="contact">
<cfset caller.mainTitle = "Contact Us">
<div id="contact_form">
<cfform method="post" action="contact2" id="usrform">
First Name<br>
<input class="textbox" type="text" name="firstName" value="First Name" onfocus="if (this.value=='First Name') this.value='';">
<br>
Last Name<br>
<input class="textbox" type="text" name="lastName" value="Last Name" onfocus="if(this.value=='Last Name') this.value ='';">
<br>
Email<br>
<input class="textbox" type="text" name="email" value="Email" onfocus="if(this.value=='Email') this.value='';">
<br>
Phone Number<br>
<input class="textbox" type="text" name="phone" value="Phone Number" onfocus="if(this.value =='Phone Number') this.value='';">
<br>
<input type="submit" class='submitBtn'>
</cfform>
<br>
</div>
<div class="commentsTop">
<p style="color:black; font-size:18px; text-align:left;">We would love to hear from you!<p><br>
<textarea class="comments" rows="10" cols="100" name="comment" form="usrform" onfocus="if(this.value=='Enter your message here...') this.value='';">Enter your message here...</textarea>
</div>
</cfcase>
<cfcase value="contact2">
<cfmail to="test#test.com" from="tester#test.com" Subject="Message From Contact Us" type="HTML">
</cfmail>
</cfcase>
I have a form that I want to have attached as the body of the email. Wasn't sure if I need to have the form as a <cfform> or if that doesn't matter.
Here is what I will do:
I will use Normal html form (cfform is also fine)
Give action to form (it can be same page or you can have separate submit page.)
On submit page I will write logic to send mail.(if its simple mail sending and nothing complex is happening then cfm page is fine otherwise CFC is preferred)
Contactus.cfm
<form method="post" action="submitform.cfm" id="usrform">
First Name<br>
<input class="textbox" type="text" name="firstName" value="First Name" onfocus="if (this.value=='First Name') this.value='';">
<br>
Last Name<br>
<input class="textbox" type="text" name="lastName" value="Last Name" onfocus="if(this.value=='Last Name') this.value ='';">
<br>
Email<br>
<input class="textbox" type="text" name="email" value="Email" onfocus="if(this.value=='Email') this.value='';">
<br>
Phone Number<br>
<input class="textbox" type="text" name="phone" value="Phone Number" onfocus="if(this.value =='Phone Number') this.value='';">
<br>
<input type="submit" class='submitBtn'>
</form>
Submitform.cfm
Make sure you are passing correct credential and server details in cfmail
<cfmail to="test#test.com" from="tester#test.com" Subject="Message From Contact Us" type="HTML">
<!--- Your message body (you can use your form variable here) --->
FistName: #form.firstName#
LastName: #form.lastName#
</cfmail>
One file solution
<form method="post" action="?">
First Name<br>
<input class="textbox" type="text" name="firstName" value="First Name" onfocus="if (this.value=='First Name') this.value='';">
<br>
Last Name<br>
<input class="textbox" type="text" name="lastName" value="Last Name" onfocus="if(this.value=='Last Name') this.value ='';">
<br>
Email<br>
<input class="textbox" type="text" name="email" value="Email" onfocus="if(this.value=='Email') this.value='';">
<br>
Phone Number<br>
<input class="textbox" type="text" name="phone" value="Phone Number" onfocus="if(this.value =='Phone Number') this.value='';">
<br>
<p style="color:black; font-size:18px; text-align:left;">We would love to hear from you!<p><br>
<textarea class="comments" rows="10" cols="100" name="comment" onfocus="if(this.value=='Enter your message here...') this.value='';">Enter your message here...</textarea>
<input type="submit" class='submitBtn'>
</form>
<cfif cgi.request_method EQ "post">
<cfmail to="test#test.com" from="tester#test.com" Subject="Message From Contact Us" type="HTML">
<!--- Your message body (you can use your form variable here) --->
<cfloop index="i" list="#Form.FieldNames#" delimiters=",">
#i# = #Form[i]#<br>
</cfloop>
</cfmail>
</cfif>
Note: the Comments field was not inside of the form
Also see:
Display CFLoop Items in Order from Form
I have a registration page that will submit all the registration values into MS SQL database. I am trying to control the size of the image that is updated to the database to be width=125 height=125. Is there a way to control that before it is submitted into the database?
Also is this the most recent way to submit it into the database?(The most recent I could find was 2011)
FORM
<form class="m-t" role="form" action="cfm/register_process.cfm" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="text" name="employee_number" class="form-control" placeholder="Employee Number" required="">
</div>
<div class="form-group">
<input type="text" name="firstname" class="form-control" placeholder="First Name" required="">
</div>
<div class="form-group">
<input type="text" name="lastname" class="form-control" placeholder="Last Name" required="">
</div>
<div class="form-group">
<input type="text" name="department" class="form-control" placeholder="Department" required="">
</div>
<div class="form-group">
<input type="text" name="position" class="form-control" placeholder="Position" required="">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email" required="">
</div>
<div class="form-group">
<input type="text" name="phone_extension" class="form-control" placeholder="Extension" required="">
</div>
<div class="form-group">
<input type="text" name="user_name" class="form-control" placeholder="User ID" required="">
</div>
<div class="form-group">
<input type="password" name="user_pass" class="form-control" placeholder="Password" required="">
</div>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Select image
<input type="file" name="filefieldname" id="filefieldname" multiple />
</span>
</span>
<input id="filename" class="form-control" type="text"/>
<span class="input-group-btn">
<span class="btn btn-primary cleared">Reset</span>
</span>
</div>
<div class="form-group">
<div class="checkbox i-checks"><label> <input type="checkbox"><i></i> Agree the terms and policy </label></div>
</div>
<button type="submit" class="btn btn-primary block full-width m-b">Register</button>
<p class="text-muted text-center"><small>Already have an account?</small></p>
<a class="btn btn-sm btn-white btn-block" href="login.html">Login</a>
</form>
Then the form calls register_process.cfm and submits to database
<cffile action="readbinary" file="#form.filefieldname#" variable="bin_filedata">
<cfquery datasource="test" name="UserRegistration">
INSERT INTO dbo.Users (employee_number, user_name, user_pass, firstname, lastname, position, email, phone_extension, department, picture)
VALUES (
, <cfqueryparam value='#form.employee_number#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form.user_name#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form.user_pass#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form.firstname#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form.lastname#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form.position#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form.email#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form.phone_extension#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form.department#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#bin_filedata#' cfsqltype='cf_sql_blob' />
)
</cfquery>
<script>
self.location="../login.html";
</script>
Unfortunately when I try to submit this I just get an all white screen and it does not update the database. I am assuming I am doing something wrong with the picture.
Getting error
Context validation error for tag cfinput.
The tag must be nested inside a cfform tag.
But I do have the form tag around the cfinput.
What could be causing the error?
Side note: before it use to be a select , but I would like to change it to a radio button.
<form method="post" action="cse_allusers_addcomment.cfm" name="add_comment">
<cfoutput>
<input type="hidden" name="txtSubmitter" value="#GetCurrentUser.emp_id#">
..more code...
</cfoutput>
<table>
<thead>
......
</thead>
<tbody>
<cfoutput>
<cfloop index="i" from="1" to="5">
<cfset question = GetEmployeeCSEDepts["csedept_question" & i][GetEmployeeCSEDepts.CurrentRow]>
<cfif question neq "">
<tr>
<td valign="top">
<cfinput type="radio" name="ratingradio" value="5">Exceptional<br>
<cfinput type="radio" name="ratingradio" value="4">Exceeds Standards<br>
<cfinput type="radio" name="ratingradio" value="3">Successful<br>
<cfinput type="radio" name="ratingradio" value="2">Needs Improvement<br>
<cfinput type="radio" name="ratingradio" value="1">Unsatisfactory<br>
<cfinput type="radio" name="ratingradio" value="0">N/A<br>
</td>
<td valign="top">#question#</td>
</tr>
</cfif>
</cfloop>
</cfoutput>
</tbody>
</table>
<cfoutput>
<p>
<br>
<textarea style="width:99%;" rows="3" name="txtPosComment"></textarea></p>
</cfoutput>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
You have it nested in a <form> tag but <cfinput> is expecting/requiring to be nested within a <cfform> tag.
By the way, why are you using <cfinput> tags anyway? You do not need to in the example given. Just use normal HTML <form> and <input> tags.
Hi I have an array of checkboxes e.g.
<input type="checkbox" name="checks[]" value="1" />
<input type="checkbox" name="checks[]" value="2" />
<input type="checkbox" name="checks[]" value="3" />
<input type="checkbox" name="checks[]" value="4" />
How do I access these in the view.py if more than one is selected?
I have tried
request.POST['checks']
but that only gives me the last value. What I want is all the ones that have been selected in a list e.g. 1,3,4
Thanks
Try this:
some_var = request.POST.getlist('checks')
some_var will contain [1,3,4] (those values that were checked)
This will fix your problem,
some_var = request.POST.getlist('checks[]')
If you write some_var = request.POST.getlist('checks') may not work properly.
this is will work:
<input type="checkbox" name="checks[]" value="1" />
<input type="checkbox" name="checks[]" value="2" />
<input type="checkbox" name="checks[]" value="3" />
<input type="checkbox" name="checks[]" value="4" />
views.py
some_var = request.POST.getlist('checks[]')