0

I have 2 submit buttons in a form. I will upload a file and save in a filepath in server when I click on one "upload" button. I want to use this file path in the code when I click on another button called "Process". When I click the process nothing is happening with dump in below code. Form submits to itself. Is there any better way to do this. Thanks in advance.

<table width="40%" border="0" style="border: 1px solid black;">

<form  name="uploadform" action="processfile.cfm" enctype="multipart/form-data"  

method="post">

 <tr>
    <td><input type="file"  name="FileContents" size="25">

        <input type="button" name="action" id="action" value="Upload">

   </td>

   </tr>

    <tr>
       <td align="middle">

      <input type="button" name="submitaction" id="process" value="Process">

       </td>

   </tr>

    </form>

 </table>

<!---Clicking the fist button to upload--->

 <cfset variables.filepath ="">

<cfif isdefined("form.action") AND form.action eq "upload">

<cffile action = "upload"  fileField = "FileContents" destination = "C:\test\" accept = "text/plain" nameConflict="overwrite">

<cfset variables.filepath= "C:\test\#cffile.serverFile#">

<!---Clicking the second button to process--->  

    <cfelseif isdefined("form.submitaction") AND form.submitaction eq "process">

              <cfdump var="#variables.filepath#">
    </cfif>
Jarede
  • 3,310
  • 4
  • 44
  • 68
  • 2
    Having two submit buttons decreases usability and complicates your code. If your user has to decide something, give them a select or radio buttons instead. – Dan Bracuk Mar 24 '14 at 01:18
  • 3
    This question appears to belong on http://ux.stackexchange.com – James A Mohler Mar 24 '14 at 01:47
  • Thanks Dan for you suggestion. – user3453252 Mar 24 '14 at 05:50
  • Thanks James, this question belongs to Coldfusion coding. I will try the forum you have given. – user3453252 Mar 24 '14 at 05:53
  • UX design is a separate issue. However there is a valid code question here, which is: why *"...nothing is happening with dump"* when the process button is clicked. The reason is a misunderstanding of the `variables` scope. It only lives for a single http request. When you resubmit the `
    `, it is a new request, so any previous values disappear.
    – Leigh Mar 24 '14 at 05:53
  • Just to make it clear: **This is a code question and _DOES NOT_ belong on ux.stackexchange.com** – Peter Boughton Mar 24 '14 at 09:44
  • @baynezy is correct about the button type issue. I was focusing on the post submit code and did not notice "process" is not a submit button. That said, the comment about the lifespan of the `variables` scope stands. I am not really sure what you are trying to accomplish .. but once you resubmit the `
    ` a second time, any previous values of `variables.filetype` will disappear.
    – Leigh Mar 25 '14 at 17:51

2 Answers2

3

The reason that your dump is not happening is because <input type="button"> does not submit forms unless you are doing that via JavaScript.

Please read Difference between <input type='button' /> and <input type='submit' />

Community
  • 1
  • 1
baynezy
  • 6,493
  • 10
  • 48
  • 73
  • ok. Thanks baynezy, this is great to know.But I tried with type=submit also. I came to know that two submit buttons are not necessary because both exists or not. So I need to accordingly. – user3453252 Mar 24 '14 at 15:01
0

You can try the below code. This dumps the value of the path as well.

<cfif isDefined('Form.tmpfile1')>
    <cfset root = ExpandPath("/Upload")><!---location where file should be uploaded--->
    <cfset filename=GetFileFromPath(Form.tmpFile1)>
    <cfset FileExt=ListLast(filename,".")>
    <cfset variables.filepath ="">      
    <cfset filename = "BidDocument1." & FileExt>

    <cffile action="upload" 
            filefield="BidDoc1" 
            destination="C:\Upload\" 
            nameconflict="overwrite" 
            accept="application/pdf,application/msword,application/vnd.ms-excel,text/plain,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
    <cfset variables.filepath= "C:\test\#cffile.serverFile#">
    <cfdump var="#variables.filepath#">
</cfif>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cffileupload Document</title>
        <form name="form1" enctype="multipart/form-data" method="post">
            <input type="hidden" name="tmpfile1" value="" />
            <input  type="file"  name="BidDoc1"  onchange="setFile(this.form);" />
            <input type="submit" name="save1" value="Upload" />  
        </form>
        <button onclick="location.href='processfile.cfm'">Process</button>
</html> 

This should help you.

Anit Kumar
  • 1,228
  • 8
  • 16