add a logout link (RT 1330 & 5518)
[freeside.git] / httemplate / elements / xmlhttp.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/xmlhttp.html',
6     # required
7     'url'  => $p.'misc/something.html',
8     'subs' => [ 'subroutine' ],
9
10     # optional
11     'method' => 'GET', #defaults to GET, could specify POST
12     'key'    => 'unique', #unique key
13
14   );
15
16 </%doc>
17 <% include( '/elements/rs_init_object.html' ) %>
18 <SCRIPT TYPE="text/javascript">
19
20 % foreach my $func ( @{$opt{'subs'}} ) { 
21 %
22 %       my $furl = $url;
23 %       $furl =~ s/\"/\\\\\"/; #javascript escape
24 %
25 %  
26
27
28     function <%$key%><%$func%>() {
29         // count args; build URL
30         var url = "<%$furl%>";
31         var a = <%$key%><%$func%>.arguments;
32
33         var args;
34         var len;
35         var content = 'sub=<% uri_escape($func) %>';
36         if ( a && typeof a  == 'object'  && a[0].constructor == Array ) {
37             args = a[0];
38             len = args.length
39         } else {
40             args = a;
41             len = args.length - 1;
42         }
43         for (var i = 0; i < len; i++) 
44             content = content + "&arg=" + escape(args[i]);
45         content = content.replace( /[+]/g, '%2B'); // fix unescaped plus signs 
46
47         if ( '<%$method%>' == 'GET' ) {
48           url = url + content;
49         }
50
51         //alert('<%$method%> ' + url);
52
53         var xmlhttp = rs_init_object();
54         xmlhttp.open("<%$method%>", url, true);
55
56         xmlhttp.onreadystatechange = function() {
57             if (xmlhttp.readyState != 4) 
58                 return;
59
60             if (xmlhttp.status != 200) {
61               alert(xmlhttp.status + " status connecting to " + url);
62             } else {
63               var data = xmlhttp.responseText;
64               //alert('received response: ' + data);
65               a[a.length-1](data);
66               if ( data.indexOf("<b>System error</b>") > -1 ) {
67                 var w;
68                 if ( w = window.open("about:blank") ) {
69                   w.document.write(data);
70                 } else {
71                   // popup blocking?  should use an overlib popup instead 
72                   alert("Error popup disabled; try disabling popup blocking to see");
73                 }
74               }
75             }
76         }
77
78         if ( '<%$method%>' == 'POST' ) {
79
80           xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
81           xmlhttp.send(content);
82
83         } else {
84
85           xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
86           xmlhttp.send(null);
87
88         }
89
90         //rs_debug("x_$func_name url = " + url);
91         //rs_debug("x_$func_name waiting..");
92     }
93 % } 
94
95
96 </SCRIPT>
97 <%init>
98 my ( %opt ) = @_;
99
100 my $url = $opt{'url'};
101 my $method = exists($opt{'method'}) ? $opt{'method'} : 'GET';
102 #my @subs = @{ $opt{'subs'};
103 my $key = exists($opt{'key'}) ? $opt{'key'} : '';
104
105 $url .= ( ($url =~ /\?/) ? '&' : '?' )
106   if $method eq 'GET';
107
108 </%init>