| home / programming / carts / chap7 / 2 | [previous] [next] |
|
|
Here we make use of the date() and mktime()
functions to display dropdowns from which the user can select the card expiration
month and year. The latter is called as
mktime(hour, minute, second, month, day, year);
and returns a Unix timestamp which is equivalent to the number of seconds elapsed since 12:00:00 AM Universal Time on 01 January 1970, also known as the Unix Epoch. It's not a good idea to omit any of these 6 arguments, all of which should be integers; doing so can lead to unexpected results. The function can take an optional seventh argument to indicate whether or not the time and date are Daylight Saving Time--use a 1 for DST and a zero for standard time. The default is zero.
<td class="cart" align="center">Month:<br />
<select name="Expiration0Month_required">
<option value=""
<?php
if( !isset($HTTP_POST_VARS["Expiration0Month_required"]) )
echo " selected=\"selected\"";
?>>[choose month:]</option>
<?php
This for() loop generates
a set of 12 <option> tags whose values are the integers
from 1 to 12, and whose text contains the three-letter abbreviations for the
months in order ("Jan" to "Dec"). We use the same technique
to set the selected option to the expiration month previously selected on
page reload as we did for the State dropdown in the Address form seen earlier
in this chapter:
for ($m=1;$m<13;$m++)
{
$month = date ("M",mktime(0,0,0,$m,1,0));
echo "<option value=\"$m\"";
if( isset($HTTP_POST_VARS["Expiration0Month_required"])
&&
$HTTP_POST_VARS["Expiration0Month_required"] == $m)
echo " selected=\"selected\"";
echo ">$month</option>\n";
}
?>
</select>
</td>
<td class="cart" align="center">Year:<br />
<select name="Expiration0Year_required">
<option value=""
<?php if( !isset($HTTP_POST_VARS["Expiration0Year_required"]) )
echo " selected=\"selected\"";
?>>[choose year:]</option>
<?php
The dropdown for the card's year of expiration is generated in a similar fashion, except that we start with the current year and display options for it and the 5 years following. As most credit and bank cards are issued for 1 to 4 year periods, this should be a sufficient span of time:
$thisyear = date("Y");
for($y=0;$y<=5;$y++)
{
$theyear = $thisyear + $y;
echo "<option value=\"$thisyear\"";
if( isset($HTTP_POST_VARS["Expiration0Year_required"]) && $HTTP_POST_VARS["Expiration0Year_required"] == $thisyear)
echo " selected=\"selected\"";
echo ">$theyear</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td colspan="2"><hr noshade="noshade" /></td>
</tr>
<tr>
<td class="cart" align="center">Card Number:<br />
<input type="text" name="Credit0Card0Number_ccnumber_required" size="16"
value="<?php report("Credit0Card0Number_ccnumber_required"); ?>" />
</td>
<td class="cart" align="center">Zip Code where you receive your statement:<br />
<input type="text" name="Cardholder0Zip0Code_uszip_required" size="10"
value="<?php report("Cardholder0Zip0Code_uszip_required"); ?> />
</td>
</tr>
<tr>
<td colspan="2"><hr noshade="noshade" /></td>
</tr>
<tr>
<td align="center">
<input type="submit" name="cc_submit" class="search" value="Submit" />
</td>
<td align="center">
<input type="reset" name="reset" class="search" value="Reset" />
</td>
</tr>
| home / programming / carts / chap7 / 2 | [previous] [next] |
Created: December 26, 2002
Revised: December 26, 2002
URL: http://webreference.com/programming/carts/chap7/2/7.html