01
| ';
10 $elTemplate .= '#{label} | ';
11 $elTemplate .= '';
12 $elTemplate .= '{error} {element} | ';
13
14 // Instantiate form
15 $myForm = new HTML_QuickForm('CreditcardForm', 'POST');
16 // new text for mandatory fields
17 $myForm->setRequiredNote('# are mandatory');
18
19 // Add header and set new template
20 $myForm->addElement('header', '', 'Creditcarddata');
21 $myForm->setHeaderTemplate($headerTemplate);
22
23 // Add text fields and set new template for each
24 $myForm->addElement('text', 'textCardholder', 'Cardholder:');
25 $myForm->addElement('text', 'textCardnumber', 'Cardnumber:');
26 $myForm->setElementTemplate($elTemplate, 'textCardholder');
27 $myForm->setElementTemplate($elTemplate, 'textCardnumber');
28
29 // Array for credit card types
30 $cardtypes = array( 'visa' => 'VISA', 'master' => 'EuroCard', 'amex' => 'American Express',);
31 // Drop-down menu for credit card types
32 $myForm->addElement('select', 'selectCardtypes', 'cardtype:', $cardtypes);
33
34 // Arrays for months and years
35 $months = array (
36 '01' => 'January', '02' => 'February', '03' => 'March',
37 '04' => 'April', '05' => 'May', '06' => 'June',
38 '07' => 'July', '08' => 'August', '09' => 'September',
39 '10' => 'October', '11' => 'November','12' => 'December'
40 );
41 $years = array ( '2003' => '2003', '2004' => '2004', '2005' => '2005' );
42
43 // Create group elements for months and years
44 $validTo[] = &HTML_QuickForm::createElement('select', 'selectValidMonth', NULL, $months);
45 $validTo[] = &HTML_QuickForm::createElement('select', 'selectValidYear', NULL, $years);
46
47 // group elements create for months and years
48 $myForm->addGroup($validTo, 'validToGroup', 'Valid to:');
49
50 // Add Submit button
51 $myForm->addElement('submit', 'submitButton','Submit Data');
52
53 // Credit card number have 16 digits
54 $cardnumber =& $myForm->getElement('textCardnumber');
55 $cardnumber->setMaxLength(16);
56
57 // Validation rules: The two text fields must be occupied
58 // Credit card number must be numeric and comprise 16 digits
59 $myForm->addRule('textCardholder', 'Please enter cardholder', 'required');
60 $myForm->addRule('textCardnumber', 'Please supply card number', 'required');
61 $myForm->addRule('textCardnumber', 'Invalid card number', 'numeric');
62 $myForm->addRule('textCardnumber', 'Card number too short', 'minlength', 16);
63
64 // Freeze form if validation succeeds
65 if ( $myForm->validate() )
66 {
67 $myForm->removeElement('submitButton');
68 $myForm->freeze();
69 }
70
71 // Display form
72 $myForm->display();
73
74 ?>