CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Query String Variable

 



etheracide
journeyman

Aug 2, 2001, 2:51 AM

Post #1 of 14 (2268 views)
Query String Variable Can't Post

Hypothetical situation: Wink
If I were creating a voting script to use as a rating system and I wanted to have one script handle all possible 'items' being rated, how would I refer to each individual 'item'?

I am using the following to set up whether the person wants to rate the item or view the ratings:


$query = $ENV{"QUERY_STRING"};
if ($query eq "vote") {
&voteform;
} else {
&view;
}


Now each item's page will have a voting box area with two links. One to send the user to the voting form within the cgi for that item and the other will open a page which will show the ratings for that item.

how about:

<form method=""POST" action="mycgi.cgi?vote&ID=itemnumber">


I am creating the voteform subroutine which will create the voting form (obviously). I would give each new item an ID number and this would open up the voting form for that particular item.

I want to have a database for each seperate item. All the rating info for that particular item will be saved into that database. I will try something like:


$ratingdatabases = "$itemnumber.txt";


I would create a database named the same number as one of the scripts. Then everything in the form such as opening the database file and writing to it will refer to the query string. But how do I isolate the ID number?

I would assume that I would have to put something in particular where i have $itemnumber which would refer to the ID number passed along in the query string.

I would need to set $itemnumber equal to that ID in the query. I will need #itemnumber throughout the script so I need to make this reference.

Hmmm I have succeeded in confusing myself with this post. heheh Hopefully someone out there can read my mind hehe. Please ask if you have any questions so that I may clarify anything necessary. Thank you.

______________________________________

#!/usr/dusnt/know/perl

use CGI;
$a = new pointless CGI();


fashimpaur
User / Moderator

Aug 2, 2001, 4:36 AM

Post #2 of 14 (2265 views)
Re: Query String Variable [In reply to] Can't Post

Etheracide,

What a mangled up, tangled up, knot!

First of all, a CGI post does not put data into the $ENV{QUERY_STRING}. It puts the data to <STDIN>. You would never see the data in your string. Read on and I will show you how to do this correctly.

Okay, trying to make sense of things let's start by asking some of the simple questions. Do you have a
basic understanding of perl modules? I ask this not to be rude but just for my information's sake.

If you do, then, as the end of your script said:

use CGI;

Here's what I mean:

#!/usr/local/bin/perl
# mycgi.cgi

use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';

my $query = new CGI;
$itemnumber = $query->param('ID');

# now you can use itemnumber anywhere you need it.

Next, don't use .txt files for your data storage. The simple reason for this is that the data can get corrupted if more than one user tries to submit the form at a time, thus potentially overwriting the changes of the second user. Also, as the text file size increases, the access time into that file also increases and the overall performance of your program will suffer. You are better off to truly create a database in Oracle, Sybase, MS SQL, or Access even. These can be set up to have row level locking so multiple people can access the data and modify their own entries without corrupting the whole data integrity.

Lastly, you should be using Perl 5.0 or higher now. That being the case, you no longer need to explicity use the ampersand (&) to indicate a subroutine is being called. Simply request the subroutine to be executed.

Like this:


#!/usr/local/bin/perl
# mycgi.cgi

# assuming you have a hidden variable in the form
# named 'Type' with a value of 'vote' or 'view', and your
# form tag should simply be:
# <form method='post' action='mycgi.cgi'>

use CGI;
use CGI::Carp 'fatalsToBrowser';
use strict;

my $query = new CGI;
my $formtype = $query->param('Type');
if ($formtype eq "vote") {
voteform;
} else {
view;
}

HTH,

Dennis

$a="c323745335d3221214b364d545a362532582521254c3640504c37292f493759214b3635554c3040606a0",print unpack"u*",pack "h*",$a,"\n\n"




etheracide
journeyman

Aug 2, 2001, 5:06 AM

Post #3 of 14 (2264 views)
Re: Query String Variable [In reply to] Can't Post

Thank you.
Well apparently I didn't say what I wanted to correctly Smile.

What I was trying to explain about the ID was simply that I would want to give each item a seperate ID number. Each Item would get its own database. I wanted to make the link to the particular vote 'page' to be <a href="http://mydomain.com/vote/voteprogram.cgi?vote&ID=123">

Then this would open up the vote page for this item.

By using something like:

$itemnumber = $query->param('ID');
$database = "$itemnumber.dat";

etc...

I am not saying that this would work as is...but this would make each item have their own database which is rather essential to the rest of the script.

I was just wonder exactly how I would go about having the ID number in the string pass along to the $itemnumber to work in the link that I posted above. I think wha tyou gave me should be what I need. I shall try anyway. Thank you for your help.


#!/usr/dusnt/know/perl

use CGI;
$a = new pointless CGI();


Pro_4
User

Aug 2, 2001, 7:15 AM

Post #4 of 14 (2259 views)
Re: Query String Variable [In reply to] Can't Post

Hey,

For you to get the vote and id number you would do something like so...


Code
#!/usr/bin/perl -w 
use CGI qw(:all);
print header;
($action, $id) = split(/\&/,$ENV{'QUERY_STRING'});
($stuff, $idnumber) = split(/\=/, $id);
print "This should be vote: $action";
print "This is the idnumber: $idnumber";

Hope that helps :)

@letters = ('A'..'Z', 'a'..'z', '1'..'5', '_');
@i = ( '15', '43', '40');
print @letters [ @i, -1, 55];


etheracide
journeyman

Aug 2, 2001, 9:57 AM

Post #5 of 14 (2255 views)
Re: Query String Variable [In reply to] Can't Post

Thank you very much again Pro 4. Just what the doctor ordered. After much tampering with my original code...I got it the way I want it.

One small problem though and I wanted to post it and see if I can get a solution to it. If not, no biggie...I can work around it =0)

ok. I have:


($action, $ID) = split(/\&/,$ENV{'QUERY_STRING'});
($stuff, $idnumber) = split(/\=/, $ID);

# Script Number. This will give script its own identity.
my $scriptnumber;
$scriptnumber = $a->param('ID');


I got everything to work, but I am trying to make it so that each ID will have its own database file. So when a visitor posts something about a particular item,. the cgi will automatically save the info to that item's database. I tried a couple things unsuccessfully.

First, I tried messing with the scalar:


$reviewdatabase = "$scriptnumber.dat";



$reviewdatabase = "$ID.dat";



$reviewdatabase = "$scriptname.dat";


In the order of how I wanted them to be. When running the script and submitting the info, a new document was created as a database, but it was called simply '.dat' and nothing else. And when called the script with a different ID number, which worked for everything else that I had set it up to do, it saved the info to the very same database.

So I did all three scalar variables the same way, but with single quotes after the = instead of double. This then created, $scriptnumber.dat, $ID.dat, and $scripname.dat. And all the info from each seperate number was posted to the same database.

Any suggestions as to how I may change this so that each $scriptnumber will have its own database?

I can work around it if I cannot find an equitable solution, but I would like to give it a try Smile

Thanks a million!

#!/usr/dusnt/know/perl

use CGI;
$a = new pointless CGI();


Pro_4
User

Aug 2, 2001, 1:22 PM

Post #6 of 14 (2249 views)
Re: Query String Variable [In reply to] Can't Post

Well everything looks right, just wondering have you tried print $scriptnumber; to see if anything actually exists. Maybe there is something wrong with the way you are getting the info. I use to have problems with just getting .dat or .txt files but i did something different about the way i was saving the files.
instead of
open (FILE, "+>> $file") || die$!;
i did
open(FILE, "+>>$file.dat") || die$!;
The first $file included the whole name but the second $file included just the part before the file extension. Maybe it was just a psychological thing or there is a difference. Who knows...

P.S. Also the reason $ID didnt work was because it would have contained id=123 or whatever, but it still should have printed that out before the .dat

@letters = ('A'..'Z', 'a'..'z', '1'..'5', '_');
@i = ( '15', '43', '40');
print @letters [ @i, -1, 55];


etheracide
journeyman

Aug 2, 2001, 7:15 PM

Post #7 of 14 (2240 views)
Re: Query String Variable [In reply to] Can't Post

I had to go and make this more difficult on everyone {cool}.

i turned this script into three seperate cgi files because it was becoming to much of a burden trying to keep it the way it was.

I now have the first script which takes the ID number from the link and creates the appropraite form. When the user fills out the info to submit, it is sent to the second file.

The second file then takes the ID number from the first file and saves the info to the proper database and then creates an HTML file which shows the visitor what they submitted.

The third one will be a view file and will show all theposts.

I am havng a slight problem though. Go figure huh...me having a problem Smile

here is what I use on the first script to get and use the ID number:


($action, $ID) = split(/\&/,$ENV{'QUERY_STRING'});
($stuff, $idnumber) = split(/\=/, $scriptnumber);

# Script Number. This will give script its own identity.
my $scriptnumber;
$scriptnumber = $a->param('ID');


and then:


my $scriptname;

if ($scriptnumber eq "1234"){
$scriptname = "Etheracidebook";
}

if ($scriptnumber eq "9362"){
$scriptname = "Securitron";
}


As you should be able to tell..I create a new conditional statement for each new item that I want to give an ID number.

Then to submit the form to the second script, I use:


print $a->startform(-action=>'http://etheracide.hypermart.net/cgi-bin/vote/postreview.cgi?ID=$scriptnumber');


How do you think I should set that $scriptnumber so that it will be passed onto the second file?

I tried using this and then changing the ENV VAR in the second file, but to no avail. Essentially I would like for them to work exactly the same way as the second file uses the ID number for the same purposes.

I have pulled an all-night on this on last night and it is slowing me down now hehehe

Thanks as always =0)

#!/usr/dusnt/know/perl

use CGI;
$a = new pointless CGI();


Pro_4
User

Aug 3, 2001, 6:09 AM

Post #8 of 14 (2233 views)
Re: Query String Variable [In reply to] Can't Post

Well if you wouldnt mind could you plz give me all three script(just join them into one big file and but dividers of some sort between each one) and i will see what i can do. I will also merge them back together so your users wont have as much to configure.

@letters = ('A'..'Z', 'a'..'z', '1'..'5', '_');
@i = ( '15', '43', '40');
print @letters [ @i, -1, 55];


etheracide
journeyman

Aug 3, 2001, 10:33 AM

Post #9 of 14 (2229 views)
Re: Query String Variable [In reply to] Can't Post

Attached is a text file containing what i have, explainations of what I have coded, and what I want the stuff to do that I didn't code yet. I was trying to get the first two parts to work before I started coding the third part. There is some info for you at the end.

You will be pleased to know that I have remembered all the stuff that you have told me about this and any other script so I am learning everything you teach heheh I taught myself a lot of Perl...but obviously never enough, but I take it all in.
I don't think I can thank you enough Smile

#!/usr/dusnt/know/perl

use CGI;
$a = new pointless CGI();


Pro_4
User

Aug 3, 2001, 2:59 PM

Post #10 of 14 (2219 views)
Re: Query String Variable [In reply to] Can't Post

Everything thing looks pretty good. The only thing i noticed is in your print statements.
just an example:
print "<a href='blah.html'>";

See you need to cancel out the = or perl interprets you want href to equal blah.html or something to that extent. So you need to do like so whenever you have a =
print "<a href=\'blah.html'>";

I may be wrong but that seemed to be one of my problems when printing out html files. Otherwise nothing is really wrong. Also good job on the notes in the file, that is one of the things i have never done good, so someone looking at my code is like "what in the world does this do... umm print "Hello World"; in a 4000 line code?"

Also the way you are learning is the same way i did. I taught myself and then came here and asked nearly the same questions you did. So i might as well spread the knowledge :)

@letters = ('A'..'Z', 'a'..'z', '1'..'5', '_');
@i = ( '15', '43', '40');
print @letters [ @i, -1, 55];


etheracide
journeyman

Aug 3, 2001, 8:12 PM

Post #11 of 14 (2213 views)
Re: Query String Variable [In reply to] Can't Post

My problem with this script is that I am not sure how I should go about sending the itemnumber from the first step to the second.

The first script takes the ID number from the URL. It then turns it from a number into a particular item (in my case $scriptname), but I am not sure of the best way to send the ID number to the second script so that it can use it.

The first script uses it primarily just to print the particular script's name to the form and the titlebar. But it needs to be passed to the second script so that script can use it to save the info to the right database.

The review form for each particular item is called up as such:


<a href="/cgi-bin/review/reviewprogram.cgi?ID=123" target="blank">


This obviously opens up the review form in a new window. The ID number is of course different in each a href for each item. The first script takes the ID number (thanks to you) and translates it to a certain item. It then prints out the item's name where I put $scriptname.

But I need to get the number from the second to the first script. I tried using:


<form action="http://mydomain/cgi-bin/reviews/reviewprogram.cgi?ID=$scriptnumber" action="POST">


But when I hit submit and it opened up to the second script, there was a blank space where i put


print "Your review of $scriptname"


And as I said before and you saw in the code, i set $scriptnumber = $scriptname.

And when i checked the database, it didn't save it correctly. It created a new database because the ID number that was originally called up in the ahref was not passed to the second script. I should be fine again once I figure out how to do that. I will keep trying but please bounce any ideas off of me. Wink

Look closely the part that i typed above in the first script...the form action to make sure that I used the best scalar to send the ID number to the second script. And then check out the whole ENV VAR part at the top of the second script. I am sure that this is where the problem lies. Thanks =0)

#!/usr/dusnt/know/perl

use CGI;
$a = new pointless CGI();


Pro_4
User

Aug 3, 2001, 8:20 PM

Post #12 of 14 (2212 views)
Re: Query String Variable [In reply to] Can't Post

just curios put do you understand what this does:
my $scriptnumber;
$scriptnumber = $a->param('ID');
?
Well it takes the value from a hidden/text/textbox or any input field named id and assigns it to $scriptnumber.

What you should do is your form info should send the data like so:
<form method=POST action = "http://etheracide.hypermart.net/cgi-bin/vote/postreview.cgi" ENCTYPE="multipart/form-data">

Also when you are posting the info on that certain script make it create a hidden box with the $idnumber in it. Name that hidden box ID so that $scriptnumber will take in that data and pass it to the next phase. If you would give me a while i could fix up your script to the way it might work :)

(This post was edited by Pro_4 on Aug 3, 2001, 7:26 PM)


etheracide
journeyman

Aug 4, 2001, 11:04 AM

Post #13 of 14 (2205 views)
Re: Query String Variable [In reply to] Can't Post

I could have sworn that I posted a reply to this, but I checked and it isn't here so maybe I closed the browser too early.

Anyway. I would be so grateful if you would be able to try to fix that problem. I am also working on your JavaScript question it the meantime, but whatever I try works, but doesn't do what you want.

Please let me know if you will be trying to get that part to work (passing the script's ID number to the second script).

I will see what I can do with your problem awhile. Thanks a ton anda half hah

#!/usr/dusnt/know/perl

use CGI;
$a = new pointless CGI();


etheracide
journeyman

Aug 4, 2001, 11:55 AM

Post #14 of 14 (2203 views)
Re: Query String Variable [In reply to] Can't Post

I was going to edit the last post, but I wasn't sure if you had already read it or not. If you had read it, then you might not know what I am going to tell you now =0)

I did it! Well I am one step closer to completion. You were totally right! The hidden field in the submit review form worked! It opened up the second script with the necessary data for it to print the $scriptname where I needed it.

Now I have to fix it so that it will create a new database for each $scriptnumber, but atleast now the necessary information is being passed to it to do that.

Thank you for the suggestion as it seems to have done the job =0)

#!/usr/dusnt/know/perl

use CGI;
$a = new pointless CGI();

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives