Spaces in a parameter

How can I send a parameter with spaces from my HTML form to the CGI script?
Who is Participating?
HedgeMazeConnect With a Mentor Commented:

Here is a subroutine that will parse the arguements (the $ENV{'CONTENT_LENGTH'} variable and perserve any parameters or variables that are defined in the HTML form.

The line "$FORM{$name} = $value;" is the coolest.  What it is basically saying is that for every FORM variable split it out so it can be used as a variable in PERL.  Say for example you have a input field in your HTML form with the name of "email".  When the Perl script reads the Content_length environment variable it will set it up so that the variable is now $FORM{email}.

Short version of the answer:  There's no need to worry about spaces as long as the Perl script can properly decode the content_length variable and the subroutien below does just that.

sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      # Un-Webify plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;
      $FORM{$name} = $value;

you dont have to worry about embeded spaces in your data.
The browser will convert spaces and any other special characters before it passes them to the CGI script.

if you have used <form MEthod=get ...> in the HTML tag, then you can, in fact see this in the URL itself!!


you form has a HTML element

<form action=/cgi-bin/CGI_script method=get>
<input type=text name=fn value="Mr Jack">
<P><input type=submit>

and you have used method=get then the URL will appear like so...
wariarAuthor Commented:
I use the POST method. Everytime there is a space in the parameter eg: "Mr Jack", it reads only "Mr".
<form method=POST action=/cgi-bin/CGI_script?fn=Mr+Jack>
If the paramater is passed by user input it will preserve the spaces.  If you do a paramater dump, it will show up with %20 replacing the spaces.  If you assign the paramater to a variable, the varaible will contain the text that the user typed with the spaces showing up as spaces.  If you are trying to specify the paramaters within a program make sure that the whole text is within quotes, if double quotes do not work then use single quotes.  If that still does not work, use %20 where you would like to have a space, since that is how it will be encoded anyway.
All Courses

From novice to tech pro — start learning today.