How to Join Member's Area Private Library Search Today's Topics p Login
Main Forums Discussion Tech Talk Mature Content Archives
   Nav Win
 Tech Talk
 Beyond the Basics
 Specific variable information...
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Follow us on Facebook

 Moderated by: Ron   (Admins )

 
User Options
Format for Better Printing EMail to a Friend Not Available
Admin Print Send ECard
Passions in Poetry

Specific variable information...

 Post A Reply Post New Topic   Go to the Next Oldest/Previous Topic Return to Topic Page Go to the Next Newest Topic 
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


0 posted 05-23-2001 03:00 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

In the last thread: http://piptalk.com/pip/Forum65/HTML/000005.html we pulled the variables from a form, split them, then printed them out using the cgi program.

HOW IN THE WORLD do I determine SPECIFIC string information, and print it out accordingly?

For example, instead of printing out the name, email address and submission, I simply want to print out the name and email for say... verification or something... heck, LOL, just to DO it... how do I do that?

THis is where the books I have are not helping me... they've shown me how to get the information and how to break it up, but how do I assign them as variables if I can't know which is which?

Peace,

C
Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


1 posted 05-23-2001 06:35 AM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

You're using a Perl package, or Library, called cgi.pm (or, at least, a part of that library) when you do the use CGI qw(:standard) at the top of your script. (BTW, the qw part of that is a derivative of our qq that we talked about before.) You then use two functions from that Library, header() and param(), to avoid re-inventing the wheel and writing your own code from scratch. The first function prints the HTTP header that tells a browser on the other end what to expect next, and the second function captures your form variables. Lots and lots and lots of modern programmers depend on prewritten Libraries like this, and there's absolutely nothing wrong with doing so.

But, uh, I don't. Or at least I very rarely do. Perl is a relatively new language to me, but I started programming back when computer memories were still measured with K's instead of M's and every single byte was a precious resource. You are using only two functions, but your server has to load the whole library, probably a hundred or more functions. Asking an old programmer to do that is like asking someone raised in the Depression to wantonly waste money; possible, yea, but it sure goes against the grain. I mention this not to discourage you from using Libraries, because they are useful and make a lot of sense with today's computers, but rather to explain why I don't have a lot of experience with them. And, at least part of what I'm about to tell you are guesses based on experience, rather than solid knowledge.

Facts, first.

You are assigning all of your form variable names to an array called keys. I know that because of the @ symbol that precedes the variable name. An array is nothing more than a list that can be referenced by number as well as name. The opposite of a list is a scalar variable, preceded by a $ sign, which must be used to reference the individual values within the array. Computers typically (almost always) start counting at zero, so that's where our array starts counting, too. Therefore:

@keys is the whole list and

$keys[0] = name of a form parameter
$keys[1] = name of a form parameter
$keys[2] = name of a form parameter

I "think" they'll be in the same order as on the form, but that's a guess. One way to cycle through all of the elements of an array is with a for loop.

for ($ii=0; $ii<3; $ii++) {
  print $keys[$ii];
}

Read that first line of the code as "for $ii equals 0 and while $ii is less than 3, increment $ii by 1." So, the first time through the loop $ii will be set to zero, and that corresponds to our $keys[0] variable. The second time through the loop $ii is one and we have $keys[1]. The third time through the loop $ii is two and we have $keys[2]. When $ii is again incremented by one it becomes three and our $ii<3 conditional fails, so we exit the loop. The only problem with a for loop is that the programmer MUST know where he wants to start and where he wants to end. In your script, you instead use a foreach loop which solves this problem. A foreach loop tells the computer "start at the beginning of the array and end when you're done," letting it figure out the tedious stuff. But we have to reference the elements of the array differently, since we no longer have our $ii counter available. That's where the key part of your foreach statement comes in. It effectively is assigning each element to that NEW variable. Don't be confused by the fact that key is almost the same as keys - it's not the same, it's an entirely new variable. It's the same as, in our for loop, saying $key = $keys[0] on our first time through the loop. Personally, I dislike using confusingly similar variable names and would probably have written it differently.

foreach $formName(@keys) {
  print
}

To me, that's easier to understand and less confusing. And it makes it more clear that each time through the loop, $formName is going to hold the name of one of our form variables.

It gets a bit trickier, though, when we start talking about the values of your form variables, rather than just the names of them. And part of that is because the cgi.pm function param() is keeping them hidden from you and only giving you a value when you specifically ask for it.

Let's strip your print statement of the HTML code for simplicity. Because Perl expands variables and function when in quotes, we can also get rid of the dot concatenation operators.

print "$key param($key)";

We already know, now, that $key holds the name of a form variable, so we know the first part of that print statement will print out the name. After running the script, you also know the second part of the print statement is the value of that form variable. Essentially, you are calling the param() function again, and saying, "Hey, what is the value of THIS element?" with THIS being determined by the name of the form variable.

Since, at this point, you are using only three form variables, we should be able to replace your whole loop with just three print statements:

print "$keys[0] param($keys[0])";
print "$keys[1] param($keys[1])";
print "$keys[2] param($keys[2])";

Cool, but that doesn't really help us much. However, since you wrote the form, you already KNOW what the form names are going to be. So you could also write it this way:

print "name param('name')";
print "email param('email')";
print "sub param('sub')";

Note that we don't have a single $ sign in our code, meaning these are all literal strings and not variables. That's why the single quotes in the call to the param function are necessary.

So, to answer your original question (finally!), to get each specific variable you simply ask the param() function to give it to you - by passing it the literal name that corresponds with the value. To do that, you MUST know the names used on the form. Ergo:

$EmailAddress = param('email');

p.s. If you'd like to know more about the other functions available to you in cgi.pm, here's a decent resource.
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


2 posted 05-23-2001 08:57 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

arrghh - I half decided to change my whole personal philosophy here... lol.

Ron, I'm sorry. You've gone to all this effort, and that's not really what I want.   It is definitely good information to have (and I think the array will prove similar anyway), but I'm in a similar boat as you inasmuch as I would rather write it all myself. This is many reasoned...

I am more than willing to use something someone else has written. If it works, it works, no ifs and buts, etc. Just look at CH - every program there, with a few javascript exceptions, are written by other people.

The problem, along with the loading part you mentioned, is lack of ability to meet specific needs. I'm a control freak, I can't help it. It's my nature... at least that's what experience and several psychological tests have shown. LOL - short of it... I'd like to see your way of doing these things. The one I posted before was done using the cgi.pm because that's where I was at in the book (and this guy is a huge proponent of the libraries available). Preferentially, I want to write it all out myself. If I come to a place where the libraries would seem prudent, THEN I can consider using them based on knowledge instead of lack of choice.

Sorry about that, I really am!

quote:
(BTW, the qw part of that is a derivative of our qq that we talked about before.)


Ok - I admit... I don't quite understand what you're saying here. Does this mean that I can't use the 'qq' as you mentioned, without including this library, or do you mean something else?

Thanks Ron - you're a diamond to this poor, struggling, wannabee perl programmer!  

C
Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


3 posted 05-23-2001 07:02 PM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

Okay, now ya lost me. I can certainly understand wanting to maintain control. But if you're not asking how to assign the form elements to variables, I'm not quite sure what you ARE asking?

As for the qw() function, I simply meant it performs a similar duty. The qq() function takes a string of words and puts them in quotes, while the qw() function take a string of words and turns them into a list (or array). It sounds a bit confusing, but it's really just trying to avoid that awkward quotation mark again. Here's one way to create an array:

@words = ("one", "two", "three");

That creates an array with three elements, but notice all the quotation marks necessary to mark each as a literal string? It's particularly easy to get confused and put one of the separating commas inside the quotes and mess everything up. So, we have the qw() to help us out.

@words = qw(one two three);

Does exactly the same thing, without a bunch of quotation characters getting in the way. That's why I called it a derivative of qq()
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


4 posted 05-23-2001 09:17 PM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

(sorry for the mess... had to disable smilies...)

ok - I follow what you've said above, and in the interm will work with that... sorry about being confusing... lack of sleep will do that.  :)

What I'm asking for, is how to do it all manually without bringing in any external libraries.

I've found this so far (though have NO idea if it works or not, as I have no clue how to create variable from the form names - like you did above with the parameter function ie: $EmailAddress = param('email') ;)

code, with comments as I interpret it:

#determines whether was posted via 'POST' or 'GET' from the form... (I assume that I
#can focus on one or the other since I'll be writing the programs specifically, but
#don't know which is best...) then assigns the results to the variable $form_info
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$form_info = $ENV{'QUERY_STRING'};
}
else {
$input_size = $ENV{'CONTENT_LENGTH'};
read (STDIN, $form_info, $input_size);
}
#splitting the input into name:value pairs and placing them in the array 'input_pairs'
@input_pairs = split (/[&;]/, $form_info);
#I think this represents assigning a name for an associative array where the
#name/values will reside
%input = ();
#saying for each pair that we find in the array "input_pairs"
foreach $pair (@input_pairs) {
        #we change the plusses to spaces
$pair =~ s/\+/ /g;
#split name and value from each other $name represents name, and $value, value
($name, $value) = split (/=/, $pair);
#decode name and value from url for the 'name'
$name =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
        #then the 'value'
$value =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
#copy name & value into hash (I have NO idea what the hash really is,
        #though I assume it's along the idea of RAM...? and truthfully, I still don't get exactly what's being formed here.
$input{$name} = $value;
}

And that's what this book has. I found this information repeated a few times in various places as I was searching last night for the answer to this, so I assume (haha) that it's viable...? Don't know... I must be one or two screws off, because I would think that I could simply call the variables by name now, but that doesn't seem to work... Sorry to be confusing,

C

[This message has been edited by Christopher (edited 05-23-2001).]

Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


5 posted 05-23-2001 09:21 PM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

BTW - thank you for the explanation... that makes sense. I like that one!  
Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


6 posted 05-24-2001 10:32 AM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

Yep, that script should work and is pretty much the same as everyone uses. To understand what it's doing (beyond the Perl syntax) takes us into the world of the Common Gateway Interface.

Before looking at what the code does, it will help to first "see" what the script is going to be seeing. When you were coming to this page you were presumably at the forumdisplay page first and, had you looked at your Location bar, should have seen something like this:

http://piptalk.com/main/forumdisplay.cgi?action=topics&forum=Interactive+Cont ent&number=65&type=t

Everything that is after the question mark (?) is the QUERY_STRING and is passed to your Perl script as part of the Environment (hence the syntax $ENV{'QUERY_STRING'} ). If you build a form, and specify METHOD=GET in your <FORM METHOD=...> tag your data will be bundled into the QUERY_STRING and submitted to your script, just as above. This is the way many Internet search engines work, such as Yahoo or Lycos. The big advantage to this method is that you can pass data to your script WITHOUT using a form, and you'll find it used a lot in our forums. The disadvantage of the GET method is that (a) it's public because everyone sees it in the Location bar, and (b) the absolute most characters that can be passed this way is 256, which is often not nearly enough.

On the other hand, if your form uses METHOD=POST then these limitations no longer exist. The data is passed not as part of the URL, but rather is sent to the script's standard input (just as if someone typed it at a keyboard). The script then reads this data from STDIN, the device name for standard input. What your script then sees can be much longer (there are still limitations, running from about 7K to 15K, depending on the browser and local memory), is entirely private, but it is still formatted exactly like the URL example above. That's important, because most of the code in your script is there to tear that string apart.

@input_pairs = split(/&/, $form_info), for example, splits the whole string into array elements every time it sees the ampersand character. Using our example URL above, the array would look something like this:

$input_pair[0] = "action=topics";
$input_pair[1] = "forum=Interactive+Content";
$input_pair[2] = "number=65";
$input_pair[3] = "type=t";

Your foreach loop then cycles through the array and again splits each of the strings every time it finds an equals sign. That's what the ($name, $value) = split (/=/, $pair); statement does. So, the second time through the loop, we should have this:

$name = "forum";
$value = "Interactive Content";

I chose the second iteration for a reason - notice that your regexp statement $pair =~ s/\+/ /g; has transformed the plus sign back into a space character. This, and the subsequent pack operations, are necessary because our data has gone through a fairly complex process called url-encoding. This process converts potentially confusing characters into special codes, like the plus sign for a space. The regexp statement and the pack operations essentially reverse the url-encoding, giving us back normal "human-oriented" data.

Once we have the $name and $value pair split and decoded, we need to decide what to do with them. Your code above uses a not uncommon technique, putting them into an associative array (starts with % sign) - which is ALSO often called a hash.

An associative array is still just an array. But where a simple array is composed of just values, an associative array is composed of both values AND the names associated with the values.

To access a simple array, we use square brackets and an index number. For example, from our first loop in your code, we ended up with $input_pair[0] = "action=topics"; - with [0] specifying which element of the array we wanted. Square brackets. A number.

To access an associate array, we use curly brackets and the name associated with the value.

If we look at the last line within your loop we see, $input{$name} = $value; - which is assigning BOTH a name and a value to an element in our associate array. Let's see what it looks like if we make a little substitution.

We know:

$name = "forum";
$value = "Interactive Content";

So, by substituting, we get:

$input{'forum'} = "Interactive Content";

The name associated with our array element is 'forum' (and the quotes, either single or double, force it to be a literal string) and the value is "Interactive Content"

If I wanted to access this member of the array later in the program, I might do something like this:

print "<p> $input{'forum'} </p>";

So, you see, you DO have access to the variables - as long as you know their associated names. Curly brackets. Name.

Make sense?

Associative arrays give us a lot of power and flexibility, allowing us to separate the data from the actions performed on the data. What this effectively means is that you can potentially design one script that handles many, many different forms - because you really don't care what the form names are going to be.

However, associative arrays are not very intuitive and not always easy to work with (looping through them can be tricky). So, many programmers avoid them and design different scripts for each form they want to process. Instead of assigning everything to a hash, they do something like this within the loop:

if ($name eq "email") {
  $Email = $value;
}
if ($name eq "sub") {
  $Sub = $value;
}

This technique requires an if block for every form variable passed, and results in "hard coding" the form names into the program. If you change your form, you have to change your Perl script to match. In spite of that, it's very, very common. Our forum software uses this technique extensively, for example.


Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


7 posted 05-24-2001 06:23 PM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

*shaking head* You really do explain things well Ron - it's a gift you have!

But - I'm still having problems... based on what I knew already and cobined with what you said... I still can't for the life of me figure out why this isn't working... I get nothing but a blank screen.
http://63.144.246.54/test/testform2.htm

The codes is as follows (I haven't changed the 'post/get' methods yet, but thank you for that clue too - for what I need, the post method seems most likely the best!):

#!/usr/local/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$form_info = $ENV{'QUERY_STRING'};
}
else {
$input_size = $ENV{'CONTENT_LENGTH'};
read (STDIN, $form_info, $input_size);
}
@input_pairs = split (/[&;]/, $form_info);
%input = ();
foreach $pair (@input_pairs) {
#change the plusses to spaces
$pair =~ s/\+/ /g;
#split name and value pair
($name, $value) = split (/=/, $pair);
#decode name and value from url
$name =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
$value =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
$input{$name} = $value;
}
#change to easier to use variables
$input{'name'} = $userName;
$input{'email'} = $email;
$input{'sub'} = $Sub;
#print to page
print "Content-type: text/html\n\n";
print qq~<html><head><title>Name & Value Pairs</title></head><body>~;
print qq~<p>$userName</p>
         <p>$email</p>
         <p>$Sub</p>~;
print qq~</body></html>~;


As you can see if you use the form at the location above... the result is nothing but a blank screen. I've beat my head over this, and everything seems like it should work, but I can't see what I've got wrong here... I don't get an error, so I can't tell...

And another question...

I like the idea of assigning them to 'hard' variables, instead of placing them in an associative array. Where do I place the if statements for that though? I tried several different ways, but they produced the same blank screen, so I dont know if what I'm doing wrong is there, or in the rest that I have up...

#!/usr/local/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$form_info = $ENV{'QUERY_STRING'};
}
else {
$input_size = $ENV{'CONTENT_LENGTH'};
read (STDIN, $form_info, $input_size);
}
@input_pairs = split (/[&;]/, $form_info);

****do I still need this?*****

%input = ();

*****           ******

foreach $pair (@input_pairs) {
#change the plusses to spaces
$pair =~ s/\+/ /g;
#split name and value pair
($name, $value) = split (/=/, $pair);
#decode name and value from url
$name =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
$value =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;

*****would it be here, pulling it from the array?*****

     if ($name eq "name") {
       $userName = $value;
     }
     if ($name eq "email") {
       $email = $value;
     }
     if ($name eq "sub") {
       $Sub = $value;
     }

*****          *****
}

*****or here, after the rest of the functions are done?****

#print to page
print "Content-type: text/html\n\n";
print qq~<html><head><title>Name & Value Pairs</title></head><body>~;
print qq~<p>$userName</p>
         <p>$email</p>
         <p>$Sub</p>~;
print qq~</body></html>~;
Thank you for all your help Ron, I hope I'm not frustrating you with these silly questions!

Peace,

C

[This message has been edited by Christopher (edited 05-24-2001).]

Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


8 posted 05-25-2001 01:53 AM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

You have a definite oopsie in this block, Chris. It's not a syntax error, but a logical one. Can you see it?

#change to easier to use variables
$input{'name'} = $userName;
$input{'email'} = $email;
$input{'sub'} = $Sub;

One of the great strengths of Perl is its flexibility. One of the worst weaknesses of Perl is its flexibility - it'll let you hose yourself without even a murmur of protest. Most languages force the programmer to define a variable before it can be used. For example, in Visual Basic you might see a line like this:

Dim userName as String

This statement allocates a specific memory location to hold a variable length string and be referenced by a specific name. If you tried to use the variable userName without defining it first, the compiler would complain. This is a "good thing" because one of the most common programming errors, not surprisingly, is the simple typographical error. If I defined the variable userName above and then, a page or two later, typed userNume = "Chris" the compiler would tell me I can't use a variable without first defining it - and I'd quickly see that userName and userNume aren't quite the same thing. Without that early warning, it can be very frustrating to later track down why userName doesn't contain what it should.

Perl doesn't give us these warnings, at least not by default (you can add a strict clause at the top of your program to turn it on, but that's another whole story for later). What you are doing is introducing three brand new variables that haven't been defined OR ever used before. Well, that raises the question, what is the value of a variable before we ever put something in it. The answer depends on the type of variable. A brand new variable used in math expression will be 0 and a new variable used in a string context will be the empty string ("").

So, your new variables hold the empty string. Let's now substitute that in your code:

#change to easier to use variables
$input{'name'} = "";
$input{'email'} = "";
$input{'sub'} = "";

You, uh, just stepped on the three elements of the associative array you worked so hard to fill. I suspect what you really wanted to do was:

#change to easier to use variables
$userName  = $input{'name'};
$email = $input{'email'};
$Sub = $input{'sub'};

You might remember from the other thread, when we were talking about HTML and quotes, I said I often use temporary print statements when debugging code. This would have been a good time to throw a few in at different steps in the code and I suspect you might have seen the problem. For example, if you printed out the values for $name and $value each time through the loop, you'd be able to see exactly what was happening in the loop (and that it worked up to that point).

Right after the two pack statements, I might have thrown in a couple of prints:

print "<p>name=$name<br>";
print "value=$value</p>";

That would have printed out six lines, showing you the three form variables as they were being built. Later, after I was sure that section of the code was working, I'd have just removed the print statements (or put a # sign in front of them to REM them into comments).

I think it might also help you see the answer to your second question. Since your new if blocks and hard names are referencing $name and $value, they MUST be within the loop where those two are changing with each iteration. If you put the if blocks outside that loop, they will only catch the very last values of $name and $value.

Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


9 posted 05-25-2001 03:26 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

Complete sense on the last... I just did a 'duh' on that one.

How would you use the pack statement there? Here's what it reads in my book.
quote:
If you work with Binary Data, you know how difficult it is to convert regular numbers to binary encodings, and vice versa. The process of converting to binary data is called data packing because you basically pack data into smaller units.

Perl can help with the conversion process through its pack and unpack functions, which feature templates (much like printf) that describe how to convert between regular numbers and their binary equivalents. A related function, ver, lets you create a binary array quickly and pick values out of it.

Ron, I'm really sorry (honestly, you have no idea how hard I've tried to figure this out myself), but it still doesn't work. The most ironic part, I think, is that this seems to be one of the biggest hitches in my learning Perl. Everything else seems to be coming fairly well... except this. And of course, it's a major player.  

Follows is the HTML code of the page which is sending the data. The string passed, I assume, will look like such: http://63.144.246.54/cgi-bin/test/test2.pl?name=Christopher+W     ard&email=chrisgw@hotmail.com&sub=testing+submission

*****

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test Form</title>
</head>
<body>
<div align="center">
<table width="500" bgcolor="#c0c0c0">
<tr><td height="25"></td></tr>
<tr>
<td width="55"></td>
<form action="../cgi-bin/test/test2.pl" enctype="multipart/form-data" method="post">
<td><strong>Name</strong></td>
<td><input type="Text" name="name" size="35"></td>
</tr>
<tr>
<td width="25"></td>
<td><strong>Email</strong></td>
<td><input type="Text" name="email" size="35"></td>
</tr>
<tr>
<td width="25"></td>
<td><strong>Sub</strong></td>
<td><textarea name="sub" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td colspan="3"><br><div align="center"><input type="Submit" value="Submit"><input type="Reset" value=" Reset"></div></td>
</tr></form>
<tr><td height="25"></td></tr>
</table>
</div>
</body>
</html>

*****

Follows, is the exact script 'test2.pl'

*****

#!/usr/local/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    $form_info = $ENV{'QUERY_STRING'};
}
else {
    read(STDIN, $form_info, $ENV{'CONTENT_LENGTH'});
}
@input_pairs = split (/[&;]/, $form_info);
%input = ();
foreach $pair (@input_pairs) {
    #change the plusses to spaces
    $pair =~ s/\+/ /g;
    #split name and value pair
    ($name, $value) = split (/=/, $pair);
    #decode name and value from url
    $name =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
    $value =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
    $input{$name} = $value;
#change to easier to use variables
$userName  = $input{'name'};
$email = $input{'email'};
$Sub = $input{'sub'};
}
#print to page
print "Content-type: text/html\n\n";
print qq~<html><head><title>Name & Value Pairs</title></head><body>~;
print qq~<p>$userName</p>
         <p>$email</p>
         <p>$Sub</p>~;
print qq~</body></html>~;


*****

I appreciate your help, and really hope I'm not frustrating you. I've seriously tried virtually everything I can think of. The worst part, is that it seems sensical that it should work as is. I can follow it and 'read' the statements, and can see absolutely no reason why it doesn't work.


[This message has been edited by Christopher (edited 05-25-2001).]

Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


10 posted 05-25-2001 03:42 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

OK - based on new information (clicking the 'string' I wrote above) I narrowed the problem down considerably... the "get" method works beautifully, which tells me that there is almost certainly an error in the if statement containing the post variable.

Working right now to see if I can figure out exactly what that is...*grin*

C
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


11 posted 05-25-2001 04:33 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

no longer grinning... for the life of me I can't figure out what I'm doing wrong.

huge, heaving sigh here... maybe I need to go to bed...
Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


12 posted 05-25-2001 05:12 AM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

Remove this from your HTML page, Chris, and it should work:

enctype="multipart/form-data"

That part is telling the CGI to expect some very complicated data (used for uploading files, like we upload photos in the forums), and your simple parsing scheme is choking on it. You need a WHOLE LOT more code to handle multipart forms. Walk first. Run later.  

Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


13 posted 05-25-2001 06:08 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

laugh

groan

*bang head*

Well, as frustrating as this has turned out to be on my side, I did learn something important... just because I'm having problems doesn't necessarily mean that it's in the script... gotta remember to keep an eye on the whole package, not just parts.

Thank you Ron, for your input and patience.

C
Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


14 posted 05-25-2001 10:48 AM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

It was fun, Chris, and I'm glad it helped.

If we could get a few more people interested in this arcane stuff, I might just end up with enough material for that book you mentioned before...  

Okay, here's a challenge for ya.

Now that you've got it working and, more importantly, understand what each section does, you should try to make it more modular. Put your code that reads and parses the incoming data stream into a separate subroutine. The idea should be that future Perl programs can use the same cut & paste subroutine, with no need to modify it. It will also give you something to build upon (as you add the code for multipart forms, for example).

You code might look something like this:

#!/usr/local/bin/perl

&ReadParse; # call to read and parse incoming data

# assign variables based on specific form
$userName  = $input{'name'};
$email = $input{'email'};
$Sub = $input{'sub'};

#print to page
etcetera etcetera
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


15 posted 05-25-2001 09:05 PM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

Cool!!!

Will do! And yes, it HAS been fun. It always amazes me how much you can learn, aside from what you're working on, when you're trying to find your way through a problem!

C
 
 Post A Reply Post New Topic   Go to the Next Oldest/Previous Topic Return to Topic Page Go to the Next Newest Topic 
All times are ET (US) Top
  User Options
>> Tech Talk >> Beyond the Basics >> Specific variable information... Format for Better Printing EMail to a Friend Not Available
Print Send ECard

 

pipTalk Home Page | Main Poetry Forums

How to Join | Member's Area / Help | Private Library | Search | Contact Us | Today's Topics | Login
Discussion | Tech Talk | Archives | Sanctuary



© Passions in Poetry and netpoets.com 1998-2013
All Poetry and Prose is copyrighted by the individual authors