http://qs321.pair.com?node_id=179273

peacemaker1820 has asked for the wisdom of the Perl Monks concerning the following question:

HI Monks!
This is the code I have so far:
print "HTTP/1.0 200 OK\n"; print "Content-Type: text/html\n\n"; print "<HTML><HEAD><TITLE></TITLE></HEAD><BODY>"; use CGI; use LWP::Simple; use URI::URL; $url = url('File_Containing_form'); $url -> query_form(text1=>'my_name', text2=> 'my_lastName); $html = get($url); print $html; print "</BODY></HTML>"

When this script executes, I have a form that i retrieved from another file. How can I put my own values in this new form when it loads?

Thanks brothers

Replies are listed 'Best First'.
Re: Populating the form
by termix (Beadle) on Jul 03, 2002 at 19:50 UTC

    I am assuming you want to show this form on a browser with some values filled in. To do this, you will need to modify the stuff returned from the get($url) call (in the $html variable). There seem to be two approaches I can think of to do this.

    If the form file is in your control, you can put in special tags (like $$USERID$$) and simply do a pattern match "substitution" to replace them with your values. Hence the form file will contain:

    <input type=text name=userid value="$$USERID$$">

    And you will use something like (untested!):

    $userid="123"; $html =~ s/\$\$USERID\$\$/$userid/g;

    And it becomes

    <input type=text name=userid value="123">

    There are HTML Template libraries that can help you do this and much much more. So try reusing those rather than coming up with your own replacement scheme if this is a sizeable project.

    If the form is not in your control, you will need to parse the form (using module like HTML::Parser or HTML::TokeParser) to find the "input" tags and then switch the value parameter with ones of your own choice. There are examples in the documentation (man pages and Internet sites) that you can use.

    -- termix

Re: Populating the form
by impossiblerobot (Deacon) on Jul 03, 2002 at 23:00 UTC
    I use HTML::FillInForm, an object-oriented module that uses HTML::Parser to do the actual parsing, and can take the data to insert into the form from a variety of sources, including CGI.pm objects.

    Impossible Robot
Re: Populating the form
by metadatum (Scribe) on Jul 03, 2002 at 19:46 UTC
Re: Populating the form
by metadatum (Scribe) on Jul 03, 2002 at 20:14 UTC
    Then you would use HTML::Defaultify like so:
    $output = "<html> <head> <title>name</title> </head> <body> <form name +=formName action="cgibin/name1.pl" method="POST"> my name is: <input type="text" name="text1" size="15"> My lastName is: <input type="text" name="text2" size=15> </form> </bod +y> </html>" $def_output = defaultify($output, text1 => "John Smith", text2 => "Smith" }
    Then $def_output would contain your code, with the proper value= tags set.
Re: Populating the form
by peacemaker1820 (Pilgrim) on Jul 03, 2002 at 20:03 UTC
    <html> <head> <title>name</title> </head> <body> <form name=formName action="cgibin/name1.pl" method="POST"> <p>my name is: <input type="text" name="text1" size="15"> <p>My lastName is: <input type="text" name="text2" size=15> </form> </body> </html>

    edited: Thu Jul 4 21:30:18 2002 by jeffa - added code tags