4.x style
[freeside.git] / httemplate / elements / form-file_upload.html
1 <%doc>
2
3 Example:
4
5   <% include( '/elements/form-file_upload.html',
6
7                 'name'      => 'form_name',
8                 'action'    => 'process/target.cgi', #progress-init target
9                 'fields'    => [ 'other', 'form', 'fields' ],
10                 'num_files' => 1, #or more
11
12                 'url' => $url
13                 #AND/OR
14                 'message' => 'Message',
15
16                 #optional
17                 'key' => 'unique_key', #for using more than once on a page
18             )
19
20 % #... 
21
22 % # num_files=>1
23   include( '/elements/file-upload.html',
24              'field'    => 'element',
25              'label'    => 'Label',
26          )
27
28 % # OR
29
30 % # num_files=>2 # or more
31   include( '/elements/file-upload.html',
32              'field'    => [ 'element', 'element2', ], #etc.
33              'label'    => [ 'Label',   'Label2',   ], #etc.
34          )
35
36
37 %>
38
39 </%doc>
40
41 <% include( '/elements/progress-init.html',
42               $opt{name},
43               $opt{fields},
44               $opt{action},
45               $msg_or_url,
46               $opt{key},
47           )
48 %>
49
50 <SCRIPT>
51
52   function <% $opt{key} %>gotUploaded(success, message) {
53
54     var uploaded = document.getElementById('uploaded_files');
55     var a = uploaded.value.split(',');
56     if (success && uploaded.value.split(',').length == <% $opt{num_files} %>){
57       process(); 
58     }else{
59       var p = document.getElementById('uploadError');
60       p.innerHTML='<FONT SIZE="+1" COLOR="#ff0000">Error: '+message+'</FONT><BR><BR>';
61       p.style='display:visible';
62       return false;
63     }
64     
65   }
66
67 </SCRIPT>
68
69 <div style="display:none:" id="uploadError"></div>
70
71 <FORM NAME     = "<% $opt{name} %>"
72       ID       = "<% $opt{id} %>"
73       ACTION   = "<% $fsurl %>misc/file-upload.html"
74       METHOD   = "POST"
75       ENCTYPE  = "multipart/form-data"
76       onSubmit = "<% $opt{onsubmit} %>return doUpload(this, <% $opt{key} %>gotUploaded)"
77 >
78
79 <%init>
80
81 #my( $formname, $fields, $action, $url_or_message, $key ) = @_;
82 my %opt = ref($_[0]) ? %{ $_[0] } : @_;
83
84 my $key = exists $opt{key} ? $opt{key} : '';
85
86 push @{ $opt{fields} }, 'uploaded_files';
87
88 my $msg_or_url = $opt{message}
89                    ? { 'message' => $opt{message},
90                        'url'     => $opt{url},
91                      }
92                    : $opt{url};
93
94 $opt{onsubmit} .= ';' if $opt{onsubmit} && $opt{onsubmit} !~ /;\s*$/;
95
96 </%init>