View Full Version : how to prevent bookings form submission for expired dates
A booking form on my site contains a number of drop down boxes for the dates, ie one for the date, one for the month and a third for the year. Unfortunately some people have started to submit reservations for dates that are in the past and so I want to rectify this.
The whole form is perl generated though the html part is written as per normal html after the print pp(
How would I add a snippet of code to make sure that any dates chosen in these drop down boxes are for the future?
Y'see when selected, each drop down is ok on its own. the problem is that when the three form an expired date, the form submission should fail with the reason why.
Bazz
Calilo
07-29-2004, 04:33 PM
sure you can, it is easy, you just have to get the current date, and then compare the date enter by the users.
the year localtime() gives, is the amount of years elapsed since 1900, so you have to add 1900 to it, in order to get the current year as in a four digit stamp.
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
$Year = 1900 + $year;
if ($formyear < $Year) { &error; }
elseif ($formmonth < $Month) { &erorr; }
elseif ($formday < $Day) { &error; }
else {
script to execute
}
hope it helps
Calilo
Thanks. I'll try that.
Bazz
Calilo, I can't seem to place it in my script correctly.
here's the script. Can you show me plz, where it should go?
#!/usr/bin/perl
use CGI qw(:standard :form);
use strict;
# The Email Address will be used to receive mail
my $recipient = 'enquiries@mydomain.com';
# The Subject Title of the mail
my $subject = 'Internet enquiry via mydomain.com';
# Path to sendmail (defualt /usr/sbin/sendmail)
my $mail_prog = '/usr/sbin/sendmail';
# Fields of the HTML form.
my @fields = ('checkInDay','checkInMonthYear','checkOutDay','checkOutMonthYear','surname','firstname','address1', 'address2','address3','zip','telephone','email','fax','numberOfSingleRooms','numberOfTwinRoomsS_occ' ,'numberOfTwinRoomsF_occ','numberOfDoubleRoomsS_occ','numberOfDoubleRoomsD_occ','AdditionalRequireme nts');
my $dpt_dat_file = './dpt.dat';
my $query = new CGI;
my $script_url = $ENV{'SCRIPT_NAME'};
my $department = $query->param('dpt');
my $section = $query->param('section');
my $action = $query->param('action');
my ($dpt,$dpt_html,$dpt_img,$dpt_name,$dpt_email,$dpt_imagemain,$dpt_thumb1,$dpt_thumb2,$dpt_thumb3,$dp t_address,$dpt_url,$dpt_hyperlink1,$dpt_hyperlink2,$dpt_hyperlink3,$dpt_image1,$dpt_image2,$dpt_imag e3,$dpt_imagelarge,$dpt_enquiries,$dpt_reservations,$dpt_intro,$dpt_facilities,$dpt_findus,$dpt_attr actions,$dpt_dining,$dpt_taxi,$dpt_taxiurl,$dpt_tellafriend,$dpt_ipix,$dpt_text);
print $query->header(-type=>'text/html');
$section = 'txt' if(!$section);
open(DPTDAT,"<$dpt_dat_file");
while(<DPTDAT>) {
chomp;
if($_ =~ /^$department\t/) {
($dpt,$dpt_html,$dpt_img,$dpt_name,$dpt_email,$dpt_imagemain,$dpt_thumb1,$dpt_thumb2,$dpt_thumb3,$dp t_address,$dpt_url,$dpt_hyperlink1,$dpt_hyperlink2,$dpt_hyperlink3,$dpt_image1,$dpt_image2,$dpt_imag e3,$dpt_imagelarge,$dpt_enquiries,$dpt_reservations,$dpt_intro,$dpt_facilities,$dpt_findus,$dpt_attr actions,$dpt_dining,$dpt_taxi,$dpt_taxiurl,$dpt_tellafriend,$dpt_ipix,$dpt_text) = split(/\t/,$_);
}
}
close(DPTDAT);
if($action eq 'result') { &result; }
else { &form; }
sub form {
print qq(
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>$dpt_name</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="http://www.mydomain.com/style_perlflypage_validated_new.css" type="text/css">
<link rel="stylesheet" href="http://www.mydomain.com/style_perlreservations_new.css" type="text/css">
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
function checkFields() {
var form = document.forms.myForm;
if (form.realname.value && form.email.value && form.body.value) {
form.submit();
}else {
alert("Please fill in all fields of the form");
}
}
</script>
<!-- START OF FORM DETAILS-->
<FORM name="myForm" METHOD="POST" ACTION="$script_url">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="result">
<INPUT TYPE="HIDDEN" NAME="dpt" VALUE="$department">
<div class="header">
<a href="$dpt_url"><img class="premiseslogo" src="$dpt_img" alt=""></a>
<a href="$dpt_hyperlink1" onFocus="if(this.blur)this.blur()"><img class="th1" src="$dpt_thumb1" alt="$dpt_name View 1" /></a>
<a href="$dpt_hyperlink2" onFocus="if(this.blur)this.blur()"><img class="th2" src="$dpt_thumb2" alt="$dpt_name View 2" /></a>
<a href="$dpt_hyperlink3" onFocus="if(this.blur)this.blur()"><img class="th3" src="$dpt_thumb3" alt="$dpt_name View 3" /></a>
</div>
<div class="textmenubkgrnd"><a class="textmenu" href="$dpt_enquiries"> Contact $dpt_name </a> <a class="textmenu" href="$dpt_reservations"> Bookings </a> <a class="textmenu" href="$dpt_intro"> Introduction </a> <a class="textmenu" href="$dpt_findus"> Find Us </a> <a class="textmenu" href="$dpt_tellafriend"> Tell A friend </a> <a class="textmenu" href="http://www.mydomain.com/sitemap.htm"> Sitemap </a> <a class="textmenu" href="http://www.mydomain.com"> Home Page </a></div>
<div class="reservationsleftdiv">
<strong>Date Of Arrival: </strong> <SELECT class="textbox"
name="checkInDay">
<OPTION value=1>1</OPTION>
<OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION>
<OPTION value=4>4</OPTION>
<OPTION value=5>5</OPTION>
<OPTION value=6>6</OPTION>
<OPTION value=7>7</OPTION>
<OPTION value=8>8</OPTION>
<OPTION value=9>9</OPTION>
<OPTION value=10>10</OPTION>
<OPTION value=11>11</OPTION>
<OPTION value=12>12</OPTION>
<OPTION value=13>13</OPTION>
<OPTION value=14>14</OPTION>
<OPTION value=15>15</OPTION>
<OPTION value=16>16</OPTION>
<OPTION value=17>17</OPTION>
<OPTION value=18>18</OPTION>
<OPTION value=19>19</OPTION>
<OPTION value=20>20</OPTION>
<OPTION value=21>21</OPTION>
<OPTION value=22>22</OPTION>
<OPTION value=23>23</OPTION>
<OPTION value=24 selected>24</OPTION>
<OPTION value=25>25</OPTION> <OPTION value=26>26</OPTION>
<OPTION value=27>27</OPTION>
<OPTION value=28>28</OPTION> <OPTION value=29>29</OPTION> <OPTION value=30>30</OPTION> <OPTION value=31>31</OPTION> </SELECT> <SELECT class="textbox">
<OPTION value="April 2004" selected>Apr 2004</OPTION>
<OPTION value="May 2004">May 2004</OPTION>
<OPTION value="June 2004">Jun 2004</OPTION>
<OPTION value="July 2004">Jul 2004</OPTION>
<OPTION value="August 2004">Aug 2004</OPTION>
<OPTION value="September 2004">Sep 2004</OPTION>
<OPTION value="October 2004">Oct 2004</OPTION>
<OPTION value="November 2004">Nov 2004</OPTION>
<OPTION value="December 2004">Dec 2004</OPTION>
<OPTION value="January 2005">Jan 2005</OPTION>
<OPTION value="February 2005">Feb 2005</OPTION>
<OPTION value="March 2005">Mar 2005</OPTION>
<OPTION value="April 2005">Apr 2005</OPTION>
<OPTION value="May 2005">May 2005</OPTION>
<OPTION value="June 2005">Jun 2005</OPTION>
<OPTION value="July 2005">Jul 2005</OPTION>
<OPTION value="August 2005">Aug 2005</OPTION>
<OPTION value="September 2005">Sep 2005</OPTION>
<OPTION value="October 2005">Oct 2005</OPTION>
<OPTION value="November 2005">Nov 2005</OPTION>
<OPTION value="December 2005">Dec 2005</OPTION>
<OPTION value="January 2006">Jan 2006</OPTION>
<OPTION value="February 2006">Feb 2006</OPTION>
<OPTION value="March 2006">Mar 2006</OPTION>
<OPTION value="April 2006">Apr 2006</OPTION>
</SELECT><br />
<strong>Date of Departure: </strong> <SELECT class="textbox"
name="checkOutDay">
<OPTION value=1>1</OPTION>
<OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION>
<OPTION value=4>4</OPTION>
<OPTION value=5>5</OPTION>
<OPTION value=6>6</OPTION>
<OPTION value=7>7</OPTION>
<OPTION value=8>8</OPTION>
<OPTION value=9>9</OPTION>
<OPTION value=10>10</OPTION>
<OPTION value=11>11</OPTION>
<OPTION value=12>12</OPTION>
<OPTION value=13>13</OPTION>
<OPTION value=14>14</OPTION>
<OPTION value=15>15</OPTION>
<OPTION value=16>16</OPTION>
<OPTION value=17>17</OPTION>
<OPTION value=18>18</OPTION>
<OPTION value=19>19</OPTION>
<OPTION value=20>20</OPTION>
<OPTION value=21>21</OPTION>
<OPTION value=22>22</OPTION>
<OPTION value=23>23</OPTION>
<OPTION value=24 selected>24</OPTION>
<OPTION value=25>25</OPTION> <OPTION value=26>26</OPTION>
<OPTION value=27>27</OPTION>
<OPTION value=28>28</OPTION> <OPTION value=29>29</OPTION> <OPTION value=30>30</OPTION> <OPTION value=31>31</OPTION> </SELECT> <SELECT class="textbox">
<OPTION value="April 2004" selected>Apr 2004</OPTION>
<OPTION value="May 2004">May 2004</OPTION>
<OPTION value="June 2004">Jun 2004</OPTION>
<OPTION value="July 2004">Jul 2004</OPTION>
<OPTION value="August 2004">Aug 2004</OPTION>
<OPTION value="September 2004">Sep 2004</OPTION>
<OPTION value="October 2004">Oct 2004</OPTION>
<OPTION value="November 2004">Nov 2004</OPTION>
<OPTION value="December 2004">Dec 2004</OPTION>
<OPTION value="January 2005">Jan 2005</OPTION>
<OPTION value="February 2005">Feb 2005</OPTION>
<OPTION value="March 2005">Mar 2005</OPTION>
<OPTION value="April 2005">Apr 2005</OPTION>
<OPTION value="May 2005">May 2005</OPTION>
<OPTION value="June 2005">Jun 2005</OPTION>
<OPTION value="July 2005">Jul 2005</OPTION>
<OPTION value="August 2005">Aug 2005</OPTION>
<OPTION value="September 2005">Sep 2005</OPTION>
<OPTION value="October 2005">Oct 2005</OPTION>
<OPTION value="November 2005">Nov 2005</OPTION>
<OPTION value="December 2005">Dec 2005</OPTION>
<OPTION value="January 2006">Jan 2006</OPTION>
<OPTION value="February 2006">Feb 2006</OPTION>
<OPTION value="March 2006">Mar 2006</OPTION>
<OPTION value="April 2006">Apr 2006</OPTION>
</SELECT><br /><br />
<strong>Surname:</strong>
<INPUT class="textbox" TYPE="text" NAME="surname" SIZE="20"><br />
<strong>First Name(s):</strong>
<INPUT class="textbox" TYPE="text" NAME="firstname" SIZE="20"><br />
<strong>Address 1:</strong>
<INPUT class="textbox" TYPE="text" NAME="address1" SIZE="20"><br />
<strong>Address 2:</strong>
<INPUT class="textbox" TYPE="text" NAME="address2" SIZE="20"><br />
<strong>Address 3:</strong>
<INPUT class="textbox" TYPE="text" NAME="address3" SIZE="20"><br />
<strong>Postcode/Zipcode:</strong>
<INPUT class="textbox" TYPE="text" NAME="zip" SIZE="20"><br />
<strong>Telephone:</strong>
<INPUT class="textbox" TYPE="text" NAME="telephone" SIZE="20"><br />
<strong>Email:</strong>
<INPUT class="textbox" TYPE="text" NAME="email" SIZE="20"><br />
<strong>Fax:</strong>
<INPUT class="textbox" TYPE="text" NAME="fax" SIZE="20"><br /><br />
</div>
<div class="reservationsrightdiv">
<select class="textbox" name="numberOfSingleRooms"> <option selected="selected">0</option><option>1</option><option>2</option><option>3</option><option>4</option></select> <strong>Single: </strong><br />
<select class="textbox" name="numberOfTwinRoomsS_occ"> <option selectED="selectED">0</option><option>1</option><option>2</option><option>3</option><option>4</option></select> <strong>Twin: (Single Occ)</strong>
<br />
<select class="textbox" name="numberOfTwinRoomsF_occ"> <option selectED="selectED">0</option><option>1</option><option>2</option><option>3</option><option>4</option></select> <strong>Twin: (Full Occ)</strong>
<br />
<select class="textbox" name="numberOfDoubleRoomsS_occ"> <option selectED="selectED">0</option><option>1</option><option>2</option><option>3</option><option>4</option></select> <strong>Double: (Single Occ)</strong>
<br />
<select class="textbox" name="numberOfDoubleRoomsD_occ"> <option selectED="selectED">0</option><option>1</option><option>2</option><option>3</option><option>4</option></select> <strong>Double: (Full Occ)</strong>
<br /><br />
<!--ADDITIONAL REQUIREMENTS-->
<strong>AdditionalRequests</strong><br />
<textarea class="textbox" cols="26" rows="6" name="AdditionalRequirements"></textarea>
<!--END OF ADDITIONAL REQUIREMENTS--><br /><br />
<input class="submitbuttons" type="button" onClick="javascript:checkFields();" value="Submit">
<input class="submitbuttons" type="reset" value="Start over" name="B2">
</form>
</div>
<div class="bottomdiv"><p class="addressbox">$dpt_address</div>
</body>
</html>
);
}
sub result {
my $msg;
$msg .= "Reservation for: $dpt_name\n";
foreach (@fields) {
$msg .= join(': ',$_,$query->param("$_"))."\n";
}
sendmail($msg);
print qq(
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>$dpt_name</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../style_perlflypage_validated_new.css" type="text/css">
</script>
</head>
<body>
<div class="header">
<a href="$dpt_url"><img class="premiseslogo" src="$dpt_img" alt=""></a>
<a href="$dpt_hyperlink1"> <img class="th1" src="$dpt_thumb1" alt="$dpt_name View 1" /></a>
<a href="$dpt_hyperlink2"> <img class="th2" src="$dpt_thumb2" alt="$dpt_name View 2" /></a>
<a href="$dpt_hyperlink3"> <img class="th3" src="$dpt_thumb3" alt="$dpt_name View 3" /></a>
</div>
<p class="textmenubkgrnd"><a class="textmenu" href="$dpt_enquiries"> Contact $dpt_name </a> <a class="textmenu" href="$dpt_reservations"> Bookings </a> <a class="textmenu" href="$dpt_intro"> Introduction </a> <a class="textmenu" href="$dpt_findus"> Find Us </a> <a class="textmenu" href="$dpt_tellafriend"> Tell A friend </a> <a class="textmenu" href="http://www.mydomain.com/sitemap.htm"> Sitemap </a> <a class="textmenu" href="http://www.mydomain.com"> Home Page </a></p>
<div class="centerdiv">
<p class="enquiryform"><br /><br /><br /><br /><strong>Thank you.</strong> Your Reservation Enquiry has been submitted to $dpt_name . They will reply to you directly.<br /><br /><a class="standardlink" href="$dpt_html">Return to $dpt_name.</a></p>
</div>
<div class="bottomdiv">
<p class="addressbox"> ♦ $dpt_address ♦ </p>
</div>
</body>
</html>
);
}
sub sendmail {
open(MAIL,"|$mail_prog -t") or die print "Fail to open sendmail: $!\n";
print MAIL "To: $dpt_email\n";
print MAIL "From: ".$query->param('email')."\n";
print MAIL "CC: $recipient\n";
print MAIL "Subject: $subject\n\n";
print MAIL $_[0];
close(MAIL);
}
Calilo
08-12-2004, 07:35 PM
Well i would put it here, but will need to have the month and year as separate variables... perl localtime function gives you the month in numeric format as 1 being january. so you have to get it that way. and the year is in 4 digit format, you will have to change that in your script. and you also need to make a sub called error, that will be displayed when someone gives you an invalid date.
#!/usr/bin/perl
use CGI qw(:standard :form);
use strict;
# Get the Date
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
$Year = 1900 + $year;
# The Email Address will be used to receive mail
my $recipient = 'enquiries@mydomain.com';
# The Subject Title of the mail
my $subject = 'Internet enquiry via mydomain.com';
# Path to sendmail (defualt /usr/sbin/sendmail)
my $mail_prog = '/usr/sbin/sendmail';
# Fields of the HTML form.
my @fields = ('checkInDay','checkInMonthYear','checkOutDay','ch eckOutMonthYear','surname','firstname','address1', 'address2','address3','zip','telephone','email','f ax','numberOfSingleRooms','numberOfTwinRoomsS_occ' ,'numberOfTwinRoomsF_occ','numberOfDoubleRoomsS_oc c','numberOfDoubleRoomsD_occ','AdditionalRequireme nts');
if (@fields[checkinyear]< $Year) { &error; }
elseif (@fields[checkinmonth] < $Month) { &erorr; }
elseif (@fields[checkinday] < $Day) { &error; }
else {
script to execute (all the script goes here)
}
OK so having copied the script but now have two errors that I dont understand what they mean.
The errors are
1. premature end of scirpt headers.
2. can't modify constsnt item in text assignment at line 7 near ");"
plz help.
# Get the Date
('$Second','$Minute','$Hour','$Day','$Month','$Year','$WeekDay','$DayOfYear','$IsDST') = localtime(time);
'$Year' = 1900 + '$year';
if (@fields['checkinyear']< '$Year') { &error; }
elseif (@fields['checkinmonth'] < '$Month') { &erorr; }
elseif (@fields['checkinday'] < '$Day') { &error; }
else {
script to execute (all the script goes here)
}
Bazz
OK. I've changed the code abit from what you sent as (perhaps), my version of perl requires a slightly different script.
This line of code is causing two errors.
1. premature end of script headers
2. can't modify constant item in list assignment at line 7 near ");"
This is the line of code causing the error:
# Get the Date
('$Second','$Minute','$Hour','$Day','$Month','$Year','$WeekDay','$DayOfYear','$IsDST')
= localtime(time);
'$Year' = 1900 + '$year';
It is meant to ensure that the dates entered by the user in a bookings form are future and not past dates. This script is supposed to use the server clock to know what time it is.
Any further help greatly appreciated.
Bazz
Maybe I should post this to a new thread but I'll try here firstly.
Might there be something I need to do to my apache server to make it work properly? I have perl and php installed and I am able to view my .pl files through it. However I have this problem:-
The localtime script that Calico posted above brings up the 500 page however, I have since seen several scripts that work and which have the script written as said by Calilo. The error logs my apache throws up are:
1. premature end of script headers -(I've no idea what this means)
2. can't modify constant item in list assignment at line 7 near ");"
Hence my question.
The correct way is as Calilo suggested (and I apologise Calilo if my posts may have sugegsted I didn't respect your knowledge).
# Get the Date
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
$Year = 1900 + $year;
if (@fields[checkinyear]< $Year) { &error; }
elseif (@fields[checkinmonth] < $Month) { &erorr; }
elseif (@fields[checkinday] < $Day) { &error; }
else {
script to execute (all the script goes here)
}
Bazz
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.