Solved

Spaces in a parameter

Posted on 2000-04-17
5
397 Views
Last Modified: 2013-12-25
How can I send a parameter with spaces from my HTML form to the CGI script?
0
Comment
Question by:wariar
5 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2723833
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!!

Eg.

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>
</form>

and you have used method=get then the URL will appear like so...

http://www.biffo.com/cgi-bin/CGI_script?fn=Mr+Jack
0
 

Author Comment

by:wariar
ID: 2723930
I use the POST method. Everytime there is a space in the parameter eg: "Mr Jack", it reads only "Mr".
0
 
LVL 84

Expert Comment

by:ozo
ID: 2723969
<form method=POST action=/cgi-bin/CGI_script?fn=Mr+Jack>
0
 

Expert Comment

by:rickhall2000
ID: 2731257
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.
0
 

Accepted Solution

by:
HedgeMaze earned 50 total points
ID: 2740590
Wariar,

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;
   }

}
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question