Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

CGI form results to XML

by spencerr1 (Novice)
on Feb 16, 2013 at 22:03 UTC ( #1019080=perlquestion: print w/replies, xml ) Need Help??
spencerr1 has asked for the wisdom of the Perl Monks concerning the following question:

This probably is not the best way to get form results to XML.

But the challenge I'm having is I would like to be able to capture mulitiple same name keys. Example

Plese enter your birth date

<INPUT TYPE="text" SIZE=30 NAME=Birthdate>

Plese enter your birth date

<INPUT TYPE="text" SIZE=30 NAME=Birthdate>

It's a long form and these same name keys are throughout the form

I made it this far in the code capturing mulitple same names

After decoding %xy:Name=Mark Shortt&Salary=$5555.00&Birthdate=04/18/58&Birthdate=04/19/59


After decoding + and &:




<Name>Mark Shortt</Name>


# Extracting the + and & and creating key/value pairs

@key_value=split(/&/, $inputstring);

foreach $pair ( @key_value){

($key, $value) = split(/=/, $pair);>/p>

$input{$key} = $value; # Creating a hash


# After decoding

print "-" x 80, "

print "After decoding + and &:

print "<response>

while(($key, $value)=each(%input)){

print "<$key>$value</$key>


print "</response>

Replies are listed 'Best First'.
Re: CGI form results to XML
by Your Mother (Bishop) on Feb 16, 2013 at 22:26 UTC
    use CGI "param"; my @birthdays = param("birthday");

    CGI. It really is that easy. Donít roll your own. You have to be an expert in all the relevant RFCs as well as browser quirks to do it well and any expert wouldnít do it because itís already done.

    Also, do *not* write your own XML. Itís similarly much more difficult to do right than is obvious. XML::LibXML or XML::Simple are typical choices and the latter can be easy if you know its limitations v your actual task.

      ++ for being able to decipher that post.

        7stud I have a question

        where do I put the code you suggested if I want these repeat HTML name and value fields (Birthdays) and all the rest of the name and value fields in the form

        #! /usr/bin/perl use CGI "param"; print "Content-type: text/html\n\n"; print "&ltresponse&gt<BR/>"; foreach my $name ( param() ) { my $values = param ($name); print "&lt$name&gt<I>$values&lt/$name&gt</I><BR>"; } print "&lt/response&gt<BR/>";
        my @birthdays = param ("birthday");

        Ha Ha

        I knew you were going to say that. I could not find the button to upload the code

        I knew you were going to say that

      Thank You for your reply. Yes I have good book Perl and XML and am working through it, there are examples of XML::Simple and XMLLib.

      Thanks again

Re: CGI form results to XML
by Your Mother (Bishop) on Feb 17, 2013 at 21:08 UTC

    Now, please! donít do this directly. The Vars() thing is a back-compat shim to perl4 days and you should always white-list and filter your user input, not blanket echo. But you can experiment with thisĖ

    use strict; use warnings; use CGI::Pretty ":standard"; use XML::Simple; if ( my %params = CGI::Vars() ) { $_ = [ split /\0/ ] for values %params; print header("application/xml"), XMLout(\%params); } else { print header(), start_html(), h1("OHAI: I CAN HAZ HTML FORMZ?"), start_form(); print p(textfield("birthday")) for 1 .. 5; print end_form(), end_html(); } __END__ perl pm-1019080 'birthday=1:356;birthday=2:365' Content-Type: application/xml; charset=ISO-8859-1 <opt> <birthday>1:356</birthday> <birthday>2:365</birthday> </opt>

    You can replace CGI::Pretty with CGI. Itís just nicer to inspect with Pretty.

      Thanks for your help, I will give this a going over and give it a go

Re: CGI form results to XML
by Anonymous Monk on Feb 18, 2013 at 08:08 UTC
    I just wanted to say that it is not guaranteed (AFAIK) that the web browser sends the form fields in the same order as enumerated in the html, or that they even send all of them (when one is unset). That is, you probably can't match a name and a birth date together. You should give the text boxes different names, grouping by entry. HTML::FormFu, particularly HTML::FormFu::Element::Repeatable might help there. (It's a pretty difficult/boring job to do it manually.)

      Iíll be damned, RFC 2388 seems to agree that itís not guaranteed, because itís left unspecified. Surprising. I do a lot of form introspection and simple CGI::Dump -> email kinds of stuff and I have never noticed any browser sending the fields back in any order other than that of the fields in the page.

      Thanks for the info

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019080]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2018-06-21 09:03 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.