You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "C.Hauser - IT assistance GmbH" <c....@itassistance.ch> on 2002/01/08 12:55:47 UTC

Form Reading

Hello


This is off topic, thanks for a direct hint to a module or an
appropriate mailing list.

I want to read an HTML form into an hash. But I don't want to use
HTML::Tree or similar DOM Object trees. I need simply all form relevant
information as an hash which is human readable. Example:

my $formfields = {
        printorder => ['Name','Request','Newsletter','Message'],
        Name => {
                tag => 'input',
                attr => { value => "bbb", type => "text", style => "width:200", class => "singleinput", size => "30" } },
        Request => {
                tag => 'select', options => [ {
                        value => "casea", selected => 1, label => "xxx" },{
                        value => "caseb", label => "yyy" },{
                        value => "casec", label => "zzz" } ],
                attr => { style => "width:200", multiple => "multiple", class => "singleinput",  size => "2" } },
        Newsletter => {
                label => "Newsletter", tag => 'input',
                attr => { value => "gggg", type => "checkbox", style => "width:22", class => "singleinput", size => "30" } },
        Message => {
                label => "Nachricht", tag => 'textarea', 
                attr => { value => "sdgdsfgdsfg", style => "width:200", class => "singleinput", rows => "5", cols => "28" } } };

I've tried HTML::Form but I found a leek of support to attributes as
'class' or 'onClick'. On the print side i can not access each input
reference in a loop.


Best Regards Christian


Re: Form Reading

Posted by will trillich <wi...@serensoft.com>.
On Tue, Jan 08, 2002 at 11:01:43AM -0500, Robert Landrum wrote:
> --
> When I used a Mac, they laughed because I had no command prompt. When 
> I used Linux, they laughed because I had no GUI.  

aha. the mac now has a cli (osX) and linux has had a gui for
quite some time (x)... :)

-- 
The only virus scanners I am aware of that run under linux are
designed to scan for Windows viruses in traffic that the linux
server is handling.
	-- Dave Sherohman, on debian-user
 
will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

Re: Form Reading

Posted by Robert Landrum <rl...@capitoladvantage.com>.
>Hello
>
>
>This is off topic, thanks for a direct hint to a module or an
>appropriate mailing list.
>
>I want to read an HTML form into an hash. But I don't want to use
>HTML::Tree or similar DOM Object trees. I need simply all form relevant
>information as an hash which is human readable. Example:


Write your own.... like me ;-)

#!/usr/bin/perl

use strict;
use Data::Dumper;
my $htmlsource = '';
while(<STDIN>) {
	$htmlsource .= $_;
}
my $formfields = {};
my @tags = split(/[\<\>]/,$htmlsource);
my $i = 0;
for ($i = 0;$i < @tags; $i++) {
	my $tag = $tags[$i];
	if($tag =~ /^(input|select|option|textarea)\b/i) {
		my $type = lc($1);

		if($type eq "input") {
			my $attr = parse($tag);
 
	push(@{$formfields->{'printorder'}},($attr->{'name'} || ''));
			$formfields->{$attr->{'name'}} = {
				'tag' => $type,
				'attr' => $attr,
				'orig' => $tag
			};
		} elsif($type eq "select") {
			my $attr = parse($tag);
 
	push(@{$formfields->{'printorder'}},($attr->{'name'} || ''));
			my $rec = {
				'tag' => $type,
				'attr' => $attr,
				'orig' => $tag,
				'options' => [],
			};

			while($tags[$i] !~ /^\/select/i) {
				my $opt = $tags[$i];
				if($opt =~ /^option/i) {
					my $tmp = parse($opt);
					$tmp->{'label'} = $tags[$i+1];
					push(@{$rec->{'options'}},$tmp);
					$i++;

				}
				$i++;
			}
			$formfields->{$attr->{'name'}} = $rec;
		} elsif($type eq "textarea") {
			my $attr = parse($tag);
 
	push(@{$formfields->{'printorder'}},($attr->{'name'} || ''));
			my $rec = {
				'tag' => $type,
				'attr' => $attr,
				'orig' => $tag,
				'options' => [],
			};
			my $val = '';
			while($tags[$i] !~ /^\/textarea/i) {
				$val .= $tags[$i];
				$i++;
			}
			$rec->{'value'} = $val;
			$formfields->{$attr->{'name'}} = $rec;
		}
	}
}

print Dumper($formfields);

sub parse {
	my $tag = shift;
	$tag =~ s/^\w+//;
	$tag =~ s/^\s+//;
	return {} unless $tag;
	my $attr = {};
	my $inquote = 0;
	my $pair = '';
	for my $char (split(//,$tag)) {
		if($char eq '"') {
			$inquote = $inquote ? 0 : 1;
			next;
		}
		if($char =~ /\s/ && !$inquote) {
			if($pair) {
				my ($k,$v) = split(/\=/,$pair);
				$v ||= 1 if($k =~ /selected|checked/i);
				$attr->{lc($k)} = $v;
			}
			$pair = '';
			next;
		}
		$pair .= $char;
	}
	if($pair) {
		my ($k,$v) = split(/\=/,$pair);
		$v ||= 1 if($k =~ /selected|checked/i);
		$attr->{$k} = $v;
	}
	return $attr;
}


Call it script.pl.

$ perl script.pl < html_page_to_parse.html

Note that it doesn't differentiate between multiple forms on a single 
page.  I'll leave that as an exercise to the reader.

Enjoy,

Rob

p.s.  This hasn't been fully tested.


--
When I used a Mac, they laughed because I had no command prompt. When 
I used Linux, they laughed because I had no GUI.  

[OT] Re: Form Reading

Posted by Matthew Pressly <mp...@claborn.net>.
At 12:55 PM 1/8/2002 +0100, C.Hauser - IT assistance GmbH wrote:
>Hello
>
>
>This is off topic, thanks for a direct hint to a module or an
>appropriate mailing list.
>
>I want to read an HTML form into an hash. But I don't want to use
>HTML::Tree or similar DOM Object trees. I need simply all form relevant
>information as an hash which is human readable. Example: [... example deleted ...]


There's a thread that looks like the same as what you're asking going on currently on the Template Toolkit mailing list under the subject "[Templates] Sticky forms with hierarchical template vars".  

A list of submissions is available (by subject) at:

http://www.template-toolkit.org/pipermail/templates/2002-January/subject.html

a few of these posts have sample code that transforms the result of a form post back into a hierarchical data structure that looks similar to your example.



Matthew Pressly