Solved

Spaces in a parameter

Posted on 2000-04-17
5
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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