Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

0o0o0o0okkkkk... anyone know the whole html variables to perl lark?

Status
Not open for further replies.

mckarl

Programmer
May 5, 1999
147
GB
hi all, <br>&nbsp;<br>i am confused to this whole passing parameters across from html to perl idea, (hey, c'mon, i'm used to COBOL!) i cant seem to get my head around the idea of passing them through, and getting them to initialize to a point that the perl code can understand what they are talking about!<br><br>so, let's say the following...<br><br>&lt;HTML&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;BODY&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;form action=&quot;eskimoes.cgi&quot; method=&quot;POST&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eskimoe type:&lt;input type=&quot;text&quot; name=&quot;esktype&quot; size=30&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/BODY&gt;<br>&lt;/HTML&gt;<br><br>and my perl code is...<br><br>#!/usr/bin/perl<br>print &quot;Content-type:html/text\n&quot;;<br>print &quot;&lt;HTML&gt;\n&quot;;<br><br>-- and this is the point where you guys tell me how to say...<br><br>the type of eskimoe you input was:$esktype<br><br>-- ok, see where i'm blowing my brain??<br><br>please help, how do i carry on my cgi scriptybabydupher to tell me what i just put in the box?<br><br>-- basically printing what i just typed into my HTML browser.<br><br>Thanks all in advance!!<br><br>Karl. <p>Karl Butterworth<br><a href=mailto:karl_butterworth@ordina.co.uk>karl_butterworth@ordina.co.uk</a><br><a href= > </a><br><i>I'm slim shadey, yes i'm the real shadey, all you other slim shadeys are just imitating; so wont the real slim shadey please stand up, please stand up, please stand up!</i>
 
I'll take a plain english stab at this.....others feel free to elaborate where I miss stuff.<br><br>When you submit an HTML page that has a &lt;form&gt; tag, the inputs (text, radio button,scrollmenu, etc.) each have a name and a value (kind'a like a hash).&nbsp;&nbsp;Depending on which &lt;form&gt; method you use in your HTML page, POST or GET, the data is shipped to the web server in one of two ways.&nbsp;&nbsp;If you use GET, you will see the names and values of the &lt;form&gt; inputs show up in the URI string in the browser.&nbsp;&nbsp;There is a max size for that URI buffer, so if you have to much data, you can loose some of it with GET.&nbsp;&nbsp;If you use POST, the name/value pairs are passed through STDIN into the CGI program.&nbsp;&nbsp;So, either way, the browser sends a list of name-value pairs to the web server.<br><br>The web server then, catches the request, identifies it as a request for an executable (CGI kind'a stuff), and passes the data via the URI string or STDIN to the CGI code.<br>The CGI then, simply cleans up the name/value pairs and stuffs them in an array.<br><br>If you are using CGI.pm, this is done when you do <FONT FACE=monospace>$query = new CGI;</font>&nbsp;&nbsp;All of the ugly stuff is in the CGI.pm box.&nbsp;&nbsp;If you are not using CGI .pm, or some other similar package/module, then you might have a sub routine that parses the data passed from the browser, cleans it up a little and puts it in an associative array (hash).<br><br>might look something like......<br><br><FONT FACE=monospace><br>sub cgidecode<br>{<br>local(%vars, $val, $key, $last, $buffer, $pair, @pairs);<br><font color=red># read data from URI or STDIN as appropriate</font><br>if ($ENV{'REQUEST_METHOD'} eq &quot;POST&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;{ read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});}<br>else { $buffer = $ENV{'QUERY_STRING'}; }<br><br><font color=red># Splitting up the data fields and store in array @pairs, they are seperated with &</font><br>@pairs = split(/&/, $buffer);<br><br><font color=red># Splitting the variable names and the values and storing them in the assoc. array %vars </font><br>foreach $pair (@pairs)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;($key, $val) = split(/=/, $pair);<br><font color=red>&nbsp;&nbsp;&nbsp;&nbsp;# get rid of URI encoding</font><br>&nbsp;&nbsp;&nbsp;&nbsp;$val =~ s/\+/ /g;<br>&nbsp;&nbsp;&nbsp;&nbsp;$val =~ s/%(..)/pack(&quot;C&quot;,hex($1))/eg;<br>&nbsp;&nbsp;&nbsp;&nbsp;if ($key eq $last) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ $vars{$key} .= &quot; &quot;.$val; }<br>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ $vars{$key} = $val; }<br>&nbsp;&nbsp;&nbsp;&nbsp;$last = $key;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><font color=red># return a hash of the name value pairs.</font><br>return(%vars);<br>}<br></font><br><br>Some where else in your CGI code you might have a line like...<br><FONT FACE=monospace>%FORM = &cgidecode;</font><br>which takes the hash built by the sub routine and calls it %FORM.<br>You can then get at a variable like......<br><FONT FACE=monospace>$esktype = $FORM{'esktype'};</font><br><br>or, if you used CGI.pm, <br><FONT FACE=monospace>$esktype = $query-&gt;param('esktype');</font><br><br>'hope this helps... <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Well since you are using the Post method, and I will assume that you will continue to use that, here is the code that you will need in order to parse (extract) the information from the form.<br><br><br><FONT FACE=monospace><br><br>read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); <font color=red>#collects the data from the form</font><br>@pairs = split(/&/, $buffer); <font color=red>#rids the data of any & and places it in an array called pairs </font><br>foreach $pair (@pairs) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;($name, $value) = split(/=/, $pair);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value =~ tr/+/ /;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$FORM{$name} = $value; <font color=red># and finally, the foreach loop says that foreach value of each form, remove spaces and store it in a varialbe name $FORM{'$name'}.</font>&nbsp;&nbsp;<br>}<br></font><br><br>Now, if you like, but do not have to, define scalars to each of the fields.<br><br>For example:<br><br><FONT FACE=monospace><br><br>$age = $FORM{'age'} <br>print &quot;you said that your age was $age\n&quot;;<br></font><br><br>Where {'age'} is the name of the html text field, such as <FONT FACE=monospace>&lt;input type=text name=age&gt;</font>.<br><br>Hope this helps. <br><br><br>The code is a little different for the Get method due to the fact that you have to get rid of all the un-needed characters in the URL string.<br><br>-Vic
 
thankyou all, i have tried both methods, and am beginning to understand the reasons and methods. I am creating a few pages/cGI's so bo doubt i'll be back in here asking for help.<br><br>Thanks again... everyone<br><br><br>Karl. <p>Karl Butterworth<br><a href=mailto:karl_butterworth@ordina.co.uk>karl_butterworth@ordina.co.uk</a><br><a href= > </a><br><i>I'm slim shadey, yes i'm the real shadey, all you other slim shadeys are just imitating; so wont the real slim shadey please stand up, please stand up, please stand up!</i>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top