How to encode HTML form in coldfusion? - coldfusion

I'm new to coldfusion.
I have page called test1.cfm
<form action = "test2.cfm" method = "post">
Type your name and hit submit:<br>
<input type = "Text" name = "txt1">
<input type = "Submit" name = "" value = "submit">
</form>
And test2.cfm
<cfoutput>
success
#form.txt1#
back
</cfoutput>
Now I want to know if we can encode the displaying of text value in test2.cfm page.
For eg. if we type <br> in the textbox, test2 page will render it as html, and I want to stop rending and it should display as text.
Thanks

It's HTMLEditFormat(), not code.

Use
#htmlCodeFormat(form.txt1)#

Related

Django dropdown select by text

I have a simple django dropdown form. I want to grab the text instead of the value.
<select name="doctors" required="" id="id_doctors">
<option value="" selected="">---------</option>
<option value="3">sometext</option>
</select>
On selection I want to use the some text in the backend.
if 'bookAppointment' in request.POST:
print(request.POST['doctors'])
print(int(request.POST['doctors'])-1)
print(Profile.objects.filter())
this prints out the value 3 is there a way to just get some text out.
3
2
<QuerySet [<Profile: some>, <Profile: some>, <Profile: some text>]>
I set it up like so:
bookAppointment = BookAppointmentForm()
# Make sure I get active doctors and doctors who have a refresh_token
bookAppointment.fields['doctors'].queryset = Profile.objects.filter(Q(is_active=True)&Q(is_doctor=True))
context['bookAppointment'] = bookAppointment
Just change the value to your text, that way the text is passed through to POST instead of the value.
Otherwise you will need to add in some controls:
if self.request.POST['doctors'] == 3:
my_var = "sometext"

coldfusion cffile upload replace no file selected

In Coldfusion, using cffile upload, when a file is selected with the browse button the name of the selected file shows next to the button. So far, so good. When I submit the file for upload, the text changes back to "no file selected". This is doubtless because we are submitting a form.
However, I'm submitting the form to the same page, and would like to see the name of the file selected instead of "no file selected" which is likely to confuse the user.
The code:
<form
enctype="multipart/form-data"
method ="post"
name ="attupload"
action ="">
<cfoutput>
<table style = "margin-left: 40%">
<tr><td>
<input name="theupload"
type="file"
style = "font-family: comic sans ms;
color: ##679C9C">
</td></tr>
</table>
<div style = "width:20%; margin:5% auto 5% 45%">
<input name = "submit"
type = "submit"
class = "submitbut"
style = "font-size: 16px"
value = "Upload File">
</div>
</cfoutput>
</form>
The words 'no file selected' seem to be part of the input name = 'upload' field, but ColdFusion evidently takes control of it, and I cannot insert anything else.
Does anyone know how I can control the 'no file selected' note, so that I can put the name of the selected file back in that spot? I have tried with javascript, but it is simply ignored.
Once a file has been selected, you can access its properties via JavaScript with the File API. You can't affect the value displayed next to the default <input type="file"> field with JavaScript.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file
You should also avoid submitting forms to the same page. You should follow the process:
Form page
HTTP POST to form processing page.
after form processing, <cflocation> (301 redirect) back to a list page or the form page.
When you post to the same page, the user can hit reload and be prompted to resubmit form data. This can be problematic in many situations. As an alternate, you can post the form data using Ajax or better, via Fetch. This avoids a re-post on page reload and redirecting away from the current screen.
Also, you should avoid naming an <input type="submit"> "submit". It can conflict with the JavaScript function submit() should you need to use it.
<input name = "submit"
type = "submit"
class = "submitbut"
style = "font-size: 16px"
value = "Upload File">

In Django how to getbinary field data from POST form

I have one HTML file, where I am using one image upload button. Now this image is stored in the MySql database as Blob. I need to get or read this image data somehow in Django through post method. Can anyone please help how to do?
Icon is defined like :
icon = models.BinaryField(null=True)
My Html:
<input type="file" id="toolicon" accept="image/*" data-type='image' >
<button id="OpenImgUpload" style="margin-left: 100px">Image Upload</button>
In JQuery:
$('#OpenImgUpload').click(function(){ $('#toolicon').trigger('click'); });
Image:
Now I want to get this file as Binary Field data. Till now I have used :
tool_icon = request.POST('toolicon', '')
tool_icon = request.POST.get('toolicon', '')
tool_icon = base64.b64encode('toolicon', '')
Nothing works ... Can any one please help me.
Uploaded files are contained in request.FILES with the key corresponding to the name attribute on the input element.
So you should add a name attribute to your input:
<input type="file" name="toolicon" ...
And then access the data using request.FILES:
tool_icon = request.FILES.get('toolicon', '')
The request must have a content type of multipart/form-data which you should set on your form:
<form enctype="multipart/form-data" ...

No need of CGI.script_name

Please refer to the following code:
<cfform method="POST" action="#CGI.script_name#">
<p>Enter your Name:
<input name="name" type="text" hspace="30" maxlength="30">
<input type="Submit" name="submit" value="OK">
</cfform>
<cfscript>
function HelloFriend(Name) {
if (Name is "") WriteOutput("You forgot your name!");
else WriteOutput("Hello " & name &"!");
return "";
}
if (IsDefined("Form.submit")) HelloFriend(Form.name);
</cfscript>
Source: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=UDFs_01.html#1167055
The code runs fine even without CGI.script_name attribute of action field. May I know why it's required then? The description says "Uses the script_name CGI variable to post to this page without specifying a URL. "
The default action of an HTML form is to submit to itself when no action is specified. See this related discussion on the topic: Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")
I always include an action, if for no other reason, to avoid confusion.

ColdFusion variable not set properly

I have a form with some html elements. It has a check box which is by default checked. On click of the submit button it calls the submitForm function
Based on the checkbox condition it has to do the action. If the checkbox is Y, then it has to do one form action and if not checked then another action. Using javascript I have checked whether its checked or not. But I am not able to set the coldfusion variable for this. Always it overwrites the variable.
Below is the code snippet
This is the ColdFusion variable which is used. This is by default set to Y
<cfset form_condn_var = 'Y'/>
function submitForm(){
if (document.getElementById('Show_Final_Model').checked)
{
form.Show_Final_Model.value = 'Y';
}
else{
<cfset form_condn_var = 'N'/>
}
}
<cfif '<cfoutput>#form_condn_var#</cfoutput>' eq 'Y'>
<form id="form1" action="test.cfm" method="POST" target="testmain">
<cfelse>
<form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">
</cfif>
It always set the variable form_condn_var as N and it goes to the else condition of the form irrespective of the condition. . But when I alert the value its comes correctly.
I cannot use hidden variable also as the form is not being called initially. Based on the checked condition only it is accessed.
Could somebody please tell me why the form_condn_var gets overwritten irrespective of the condition being checked.
Or is there any other way I can achieve this?
Thanks in advance
Short version
If I have understood your logic correctly then you can replace all of what you pasted with the following <cfif>
<cfif IsDefined('form.Show_Final_Model') AND form.Show_Final_Model EQ 'Y'>
<form id="form1" action="test.cfm" method="POST" target="testmain">
<cfelse>
<form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">
</cfif>
Explanation
Your main problem here is that you have got Javascript outside of tags with a coldfusion within them and seem to be confusing what each language does.
First of all Coldfusion renders the HTML. As far as Coldfusion is concerned, the javascript if statement is just text, and so it sees the following logic
<cfset form_condn_var = 'Y'>
<cfset form_condn_var = 'N'>
<cfif form_condn_var EQ 'Y'> <!--- form_condn_var === '<cfoutput>#form_condn_var#</cfoutput>' but much cleaner --->
.......
<cfelse>
.......
</cfif>
In turn leading to the following HTML being rendered
function submitForm(){
if (document.getElementById('Show_Final_Model').checked)
{
form.Show_Final_Model.value = 'Y';
}
else{
}
}
<form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">
I suspect in your example you trimmed out some logic though as otherwise that Javascript would be output as plaintext, as it is not within <script> tags.
If you are submitting your checkbox and then setting the form action for the next form (form 2 - after you've chosen the checkbox) I think what you want to do is the following:
<cfparam name="form.Show_Final_Model" default="N"/>
<cfif form.Show_Final_Model IS 'Y'>
<form id="form1" action="test.cfm" method="POST" target="testmain">
<cfelse>
<form id="form1" action="<cfoutput>#something#</cfoutput>"
method="POST" target="_blank" onSubmit="">
</cfif>
This would be on the handler page (after the form is submitted). You would not need the JS function.
If however you are trying to change the action param of your form to something else based on the check or uncheck of the checkbox (in other words - withing the same form) then ALL of your code needs to be javascript and CF has little to do with it. Set 2 variables - action a and action b, check the value of the checked form element and change the form.action value to whichever one you want.
But most importantly get settled in your mind on what is "server side" and what is "client side" .. that's where you are slipping up. good luck :)
I'm not sure if I'm understanding the question correctly, but it would seem that this situation could be handled purely on the action page based on conditional processing based on the result of the form checkbox value. Instead of sending the form to two different action templates based on the value of the checkbox, just process the form accordingly on the action template based on conditional processing of the checkbox form value.