Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print count in hidden form field and then...

Status
Not open for further replies.

smashing

Programmer
Oct 15, 2002
170
US
Hi! I've set up a website where visitors make reservations for a bus trip. They fill out a form with all their necessary info etc. When the form is submitted a CGI script does 2 things.
1) Sends parsed output of form via SENDMAIL to reservations dept of bus company, and
2) Redirects the browser to a thank you page .
All that works fine.
Question
I now want every person to get a unique reservation number.
(If anyone has an easier way let me know, but) I have thought of a way of doing it like this:
1) Utilizing a simple text counter to count hits to the page containing the form.
2) This need not be displayed at this time so I would want the count printed in a hidden form field
3) Once 'submit button' is clicked, that number should also be submitted both in the email and in the Thank You page to the user's browser. If anyone can help me I'll sure thank them!!! By the way tried using Matt's simple counter from scriptarchive.com, but can't get it to work. PLEASE HELP!!!

 
Just one possible alternative off the top o' my head:

You could try using a flatfile (.txt, .log, whatever) to track numbers. Every time the form is submitted have it open the 'log' file, find the highest current number, and increment that number by 1. Then append that number into the log file and include it in your email...

To prevent duplicate numbers (i.e., multiple people submit at the same time - some may end up with the same number), you could use the
Code:
flock
function during the open procedure. This would prevent the other script calls from opening the file until the 1st one is finished with it.

[Make sure your server supports flock before you use it!]

mmm, that's all. [hth] Notorious P.I.G.
 
Save yourself, and your server, a lot of work and eliminate step 2 and part of step 3. Let your CGI script do the work of assigning the number AFTER the form is submitted. (There's no need to find the number first, then send it back again as part of the form data.) After your script does all the work, you can send an email and render a page with the reservation number. There's always a better way...
 
Thanks tviman!
You put it into perspective, and thanks to you, I made my own counter!!!
(It might be simple to you, but to a beginner - the sense of satisfaction and achievement that you feel when you get your first anything in CGI to actually work is like nothing in the world - I did a little jig around my desk when it happened!)
I need some help on one last part of the script here.

This is a snippet of my script:

open (COUNT, &quot;<../cgi-bin/count.txt&quot;) ||Error('open', 'file');
flock (COUNT, 1) ||Error('lock', 'file');
my $counter = <COUNT>;
close (COUNT);
++$counter;
open (COUNT, &quot;>../cgi-bin/count.txt&quot;) ||Error('open', 'file');
flock (COUNT, 1) ||Error('lock', 'file');
print COUNT &quot;$counter&quot;;
close (COUNT);
open (THANK, &quot;<../thankyou1.html&quot;) ||Error('open', 'file');
flock (THANK, 1) ||Error('lock', 'file');
$thanks = <THANK>;
#Problem with this line is that it only throws the heading of THANK into $thanks, I wan't it to put the whole html document in there. Sombody told me I have to use a loop, if this i so how is it done?#
$thanks =~ s/\d{3}/$counter/;
close (THANK);
open (THANK, &quot;>../thankyou1.html&quot;) ||Error('open', 'file');
flock (THANK, 2) ||Error('lock', 'file');
print THANK $thanks;
close (THANK);


Thanks and waiting for reply.
 
smashing....

after you've assigned/picked a unique number, you'll have to add your html page as part of your script as follows:

open (COUNT, &quot;<../cgi-bin/count.txt&quot;) ||Error('open', 'file');
flock (COUNT, 1) ||Error('lock', 'file');
my $counter = <COUNT>;
close (COUNT);
++$counter;
open (COUNT, &quot;>../cgi-bin/count.txt&quot;) ||Error('open', 'file');
flock (COUNT, 1) ||Error('lock', 'file');
print COUNT &quot;$counter&quot;;
close (COUNT);

print &quot;Content-type: text/html\n\n&quot;;

print <<EOF;
<HTML>
<BODY>


... insert your html here

when you want to display the reservation number, use your counter variable like this example:

<font face=&quot;arial&quot; size=&quot;2&quot;>Your reservation number is $counter</font>

</BODY>
</HTML>
EOF


Note the ending EOF does not have a semi-colon after it. Also, make sure there's a blank line after the EOF.

Because your script resides in a cgi-bin directory, you'll have to provide the complete path to any image, javascript, or other files that reside in your htdocs or like this: <IMG SRC=&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; BORDER=&quot;0&quot;>

It's easiest to create a variable for each image and link. I do something like this:

my $img1 = &quot;my $img2 = &quot;my $lnk1 = my $img1 = &quot;
then you can call them like this:

<IMG SRC=&quot;$img1&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; BORDER=&quot;0&quot;>
<A HREF=&quot;$lnk1&quot;>Next Page</A>

You get the idea. Makes it easier to maintain the code and is easier to read. There's always a better way...
 
Hi tviman....
Thanks man!
Before I try it your way, I wanna ask if it might not be easier to somehow do it my way. (the nerve of me to ask!).
If you look at my script there's this part:
open (THANK, &quot;<../thankyou1.html&quot;)
that opens the comlete html document that I want and then this part:
$thanks = <THANK>;
that's supposed to get all the information in THANK & throw it into $thanks
Then, there's this part:
$thanks =~ s/\d{3}/$counter/;
that searces $thanks for any 3 digits and replaces it with $counter
Finally there's:
open (THANK, &quot;>../thankyou1.html&quot;)
and:
print THANK $thanks;

Only problem I have is with the middle stage:
$thanks = <THANK>;
Instead of getting all the info of THANK into $thanks, it only gets the header. Reason I wanna do it my way is that I can allways point it do differnt html page.

Thanks again & waiting to hear from you shortly.
 
Because you're script is in perl, you have to tell the server what it's supposed to do with the script - thus the need for the statement: print &quot;Content-type: text/html\n\n&quot;;

This tells the server that you want to render an HTML page. Without it, nothing happens. Additionally, you need this method to show you're reservation number (which is a scalar variable). There's always a better way...
 
Hi TViman!
Did it your way (EOF) and everything works fine. Only problem now, (hopefully the final one) is that viewers get a thank you page including a reservation number, now if they choose to hit refresh button the script is activated again and the counter incremented with 1 ,resulting in a new reservation number. I suppose I can get the script to print everything into a page and then have it redirect viewers to that page but even then if viewers get their reservation number and meanwhile someone else makes a reservation, 2 minutes later the first viewer hits refresh he'll get a reservation number incremented twice. Limiting reservations to one per each IP address is also not an option because I want people to be able to make 2 consecutive reservations if they do so wish as long as they fill out the form. Is there some sort of code maybe a JavaScript to disable the refresh button?
 
smashing...

Couple of things you can do depending on the planned longevity of the site. First, make sure the script is activated only when the user clicks a sumbit button. After that, take them to a Thank You page, or something. Next, make sure that when the first page loads, all input fields are set to blanks, zeros, or nulls - whatever you want. That way, if the user hits the back button, they'll have to fill in the form again.

You could also set a cookie and then ask the user if they really want to make a second (or third...) reservation. If the user doesn't allow cookies then you'll have to jump some bigger hurdles.

If this is a long-term site, then I'd be inclined to use a database with fields for name, reservation number, date stamp, and number of reservations, and whatever else you want to trap. Then you could query the database and if their number of reservations is > 1, you'd ask them if they really want to do this. (If they're not in the file, then it wouldn't make any difference.)

Using IP addresses is totally out of the question. Most users will have &quot;virtual&quot; IP addresses that will change each time they log on to the 'net. There'd be no way to distinguish unique users.

Hope this helps you. Sounds like you've got most of the kinks worked out. There's always a better way...
 
Hi Tviman
Thanks for your response, & thanks for the compliment. Problem isn't when viewers hit the back button. Problem is when refreshing the thank you page. Since the script is not redirecting them to a different page ,but rather the script in itself is displaying the html code (using EOF like you wrote, or rather the here document as it's called in Perl) then if refreshed, the script will repeat itself entirely, doing all the functions it did the first time (except for the form parsing since it is now not coming from a form).

I still wanna go back and try opening a document and throwing it into a @ so that I get all of it in, instead of a $ where It just gets the title in. I need help with that though. Thanks & waiting for reply.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top