Solved

Spaces in a parameter

Posted on 2000-04-17
5
394 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
Comment Utility
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
Comment Utility
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
Comment Utility
<form method=POST action=/cgi-bin/CGI_script?fn=Mr+Jack>
0
 

Expert Comment

by:rickhall2000
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 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

10 Experts available now in Live!

Get 1:1 Help Now