You need 2 things: a form and a script that processes it.
Actually you can also have the script create the form, which lets you have anything in one place.
Below is a simple example. It uses CGI to process generate and process the form, and XML::Simple to turn the parameters into XML. You obviously need to have the script setup so it can run as a cgi on your server. Also the data should not be in the web tree, and beware of the security implications of letting an outside client create data on your machine.
#!/usr/bin/perl -w
use strict;
use CGI qw/:standard :cgi-lib/;
use XML::Simple;
# the web server needs create and write access to that file
# it should probably not be in the web tree (under DocumentRoot
# or any accessible directory like cgi-bin and al. under Apache)
my $OUT= "/web/data/out.xml";
# dispatch table <function param> => <sub that processes it>
my %process= ( display_form => \&display_form,
process_form => \&process_form,
);
my $function= param( 'function') || 'display_form'; # display_form is
+the default
Delete( 'function'); # we don't need to save function in the XML, so l
+et's remove it
if( my $process= $process{$function})
{ $process->(); }
else
{ display( p( h2( "Error")), p( "wrong function '$function'") ); }
exit;
# the interesting part, gets the params and saves them as XML
sub process_form
{ # from CGI get all the params
my %params = Vars;
# only useful if you have checkbox_group (that can take severa
+l values
# for a single param). There might be a better way, but this s
+houd work
foreach my $param (%params)
{ next unless( defined $params{$param}); # to avoid warni
+ngs when the param is not filled
my @values= split("\0", $params{$param}); # multiple v
+alues are separated by a null char
if( @values > 1) { $params{$param}= [ @values ] }
}
# from XML::Simple turn them into xml read the docs!
my $xml= XMLout( \%params, noattr => 1, rootname => 'data', xmlde
+cl => 1 );
# I am being extra-paranoid here, but you never know
# notethat this sends an Internal Error to the client
open( OUT, '>', $OUT) or die "cannot open $OUT: $!";
print( OUT $xml) or die "could not print in $OUT: $!";
close OUT or die "could not close $OUT: $!";
# send back an acknowledgement and the data (escaped, in pre tags)
display( p( "OK, XML data saved in $OUT"), pre( escapeHTML( $xml))
+);
}
# this is just an example, the generated form can be in a separate fil
+e,
# or generated using a templating module (see Text::Template or HTML::
+Template)
# the form here is straight out of the CGI docs
sub display_form
{ display( h1('A Simple Example'),
start_form,
hidden( function => 'process_form'),
"What's your name? ",textfield('name'),p,
"What's the combination?", p,
checkbox_group(-name=>'words',
-values=>['eenie','meenie','minie','moe'
+],
-defaults=>['eenie','minie']), p,
"What's your favorite color? ",
popup_menu(-name=>'color',
-values=>['red','green','blue','chartreuse']
+),p,
submit,
end_form,
hr
)
}
# a helper sub that wraps the text to be displayed
sub display
{ print header, # HTTP header
start_html('A Simple Example'), # title
@_, # what we want to display
end_html;
}
|