What you are describing would be done with client side scripting.
There are plenty of tutorials and code exmples of either of these options available for a search of your favorite search engine.
I was wondering if Ajax would be a good solution. Can you tell me this
? I have a file loader I use all the time, but on this server, it's not working properly. Can you look at my code and possibly tell me why? I
know this is a lot of code I'm pasting, but it is pretty strait forward. It doesn't thrown an error, it just doesn't load the file
I would rather use this, I have it all written:
<!--- form submitted --->
<!--- set file uploading vars --->
<cfparam name="fileuploaded" type="boolean" default="false">
<cfparam name="uploadedfile" default="">
<cfset pathToFile = "c:\Inetpub\wwwroot\website\img\babies">
<!--- if a file has been selected --->
<!--- try uploading new file --->
<cffile Action="upload" filefield="MYFile" accept="image/gif,
image/jpg, image/jpeg, image/pjpeg"
<cfset fileuploaded = true>
<cfset uploadedfile = cffile.serverfile>
<!--- if upload did not suceed, reset file uploading vars --->
<cfset fileuploaded = false>
<cfset uploadedfile = "">
<!--- this can be further enhanced by setting some var to hold error
message and return it to user --->
<cfif form.id gt 0><!--- we are updating an existing record --->
<!--- if new file upload was successful and the feature has an image
associated with it - delete old image --->
<cfif fileuploaded is true AND len(trim(form.oldimage))>
<cfif FileExists(pathToFile & form.oldimage)>
<cffile action="delete" file="#pathToFile & form.oldimage#">
baby_port.dob=<cfqueryparam cfsqltype="CF_SQL_DATE" value="#form.edit1#">,
baby_port.Fname=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Name#">,
baby_port.Lname=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Lname#">,
<cfif fileuploaded is true>
baby_port.MYFile=<cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#">,
baby_port.Body=<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#form.PDSeditor#">,
baby_port.weight=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.weight#">,
baby_port.TimeB=<cfqueryparam cfsqltype="CF_SQL_TIME" value="#form.tob#">
WHERE ID = <cfqueryparam value="#form.ID#" cfsqlType="CF_SQL_INTEGER">
<cfelse><!--- we are inserting a new record --->
INSERT INTO baby_port
(dob, Fname, Lname, MYFile, Body, weight, TimeB)
(<cfqueryparam cfsqltype="CF_SQL_DATE" value="#form.edit1#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Name#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Lname#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#" null="#NOT fileuploaded#">,
<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#form.PDSeditor#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.weight#">,
<cfqueryparam cfsqltype="CF_SQL_TIME" value="#form.tob#">)
this is the 2 fields on the form that is submitting the file:
<input type="hidden" name="oldimage" value="#MYFile#">
<input name="MYFile" type="file" id="MYFile">
I can make more available if you need it, I didn't want to unload a ton of code on you.This is a stand alone server running coldfusion 8.1 standard if that makes a difference, it is not a shared environment. I have this code working on shared environments.
sorry about that last post. I'm on a different system on the road
. I had forgotten my main account. It's mine.
Nothing jumps out at me as problmatic and if you have the same code working in other places then that should be fine.
There is probably some simple syntax error that is getting trapped by your <try...><cfcatch...> block. The way your block is written, it assumes ALL errors are a problem with the upload file. It does nothing with the exact error information and will just "not upload the file".
The quick fix would be to add a <cfdump var="#cfcatch#"> line inside the <cfcatch...>...</cfcatch> block. If you do that, there is a very good chance it will dump out an actual error and that this error is some simple syntax problem related to you copying this code from another system and not quite modifying it properly.
A more robust fix, would be to add logic to your <cfcatch..> block that logs or broadcasts this error to you or other devleopers in a more permant maner so that in the future, anything unexpected can be more easilty diagnosed.
That worked! I forgot to put in
what a silly mistake! How would you suggest putting a safty net in this code? Just currious if you had a good idea.
also, one last question for this area and I have it.
I'm trying to identify the ID to set off code that says you are either editing an existing record, or adding a new one. what am I doing wrong, it won't catch it properly. this is the code:
You are Editing: #Fname# #Lname#
Add a New Born
my form fields are here and file loader
I tried NOT isDefined and switched it around.. and it still isn't catching properly. It's driving me nuts. It's a small thing, but helps idiot proof the section. am I doing something wrong? this is what comes back from the url:
this is my query at the top of the babyPort-Edit.cfm
<cfparam name="url.ID" type="integer" default="0">
<cfparam name="ID" type="integer" default="#url.ID#">
<cfparam name="dob" default="">
<cfparam name="Fname" default="">
<cfparam name="Lname" default="">
<cfparam name="MYFile" default="">
<cfparam name="Body" default="">
<cfparam name="Weight" default="">
<cfparam name="TimeB" default="">
<cfif url.ID GT 0>
<cfquery name="babyMAN" datasource="#APPLICATION.dataSource#" maxRows=1>
SELECT dob, Fname, Lname, MYFile, Body, Weight, TimeB, ID
WHERE ID =<cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">
<cfif babyMAN.RecordCount EQ 1>
<cfset ID = babyMAN.ID>
<cfset dob = babyMAN.dob>
<cfset Fname = babyMAN.Fname>
<cfset Lname = babyMAN.Lname>
<cfset MYFile = babyMAN.MYFile>
<cfset Body = babyMAN.Body>
<cfset Weight = babyMAN.Weight>
<cfset TimeB = babyMAN.TimeB>
I'm confused at what I missed here.
thank you for your help. Sometimes I look at this too long and miss simple stuff. annoying actually.
To make your error handling more robust. In the <cfcatch...> block write the error data to a log file or email to some address where it can be retreived for later use incase of a need to debug the system.
If you do not want to deal with data for every error, includeing the user error of them not uploading a file, look into the "type" paramerter of the <cfcatch...> tag. You can create one <cfcatch...> block that traps the user empty file error that simple displays the current message and does nothing else. Then you can have another <cfcatch...> block that traps all the other error types and logs or sends the information for debugging purposes.
For the second question... What is the relationhship of these two lines of code.
<cfparam name="url.ID" type="integer" default="0">
If the if statement is on the same page and comes after the <cfparam...> statement, then the url.id varaible is always going to be defined, the cfparam tag is going to define it if it is undefined.
If this is the case you probably want the same <cfif url.id GT 0> that you use in other parts of your code.
Thank you very much! That worked.
I'm going to write up something for the cfcatch tonight. That's a good idea.