Solved

Spaces in a parameter

Posted on 2000-04-17
5
395 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

Title # Comments Views Activity
Simple JavaScript form question 4 64
exchange, scripts 3 60
Convert MSI to MSM 1 49
powershell command to get a dll version remotely for many server 12 13
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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 …

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now