form_example.php 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
include("form_defs.php");

#
# Only known and logged in users.
#
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();

$optargs = OptionalPageArguments("submit",      PAGEARG_STRING,
				 "formfields",  PAGEARG_ARRAY);

19 20
PAGEHEADER("Silly Forms example");

21 22 23
# The form attributes:
$form = array('#id'	  => 'form1',
	      '#caption'  => 'My Form',
24 25
	      # XXX Only use enctype when doing a file upload. See below.
	      '#enctype'  => 'multipart/form-data',
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	      '#action'   => 'form_example.php');

# A set of form fields.
$fields = array();

# Text field.
$fields['slot1'] = array('#type'        => 'textfield',
			 '#label'       => 'Slot1',
			 '#value'       => 'Hello World',
			 '#size'        => 60,
			 '#maxlength'   => 64,
			 '#description' => 'Alphanumeric, No Blanks'
			 );

# Password
$fields['slot2'] = array('#type'	 => 'password',
			 '#label'        => 'Password',
			 '#required'     => TRUE,
			 '#footnote'     => 'This is a footnote',
			 '#size'         => 8);

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#
# File Upload. You must set '#enctype' in the form array above.
#
# Files are special cause PHP puts all the stuff into the $_FILES
# superglobal. So, there is nothing in $formfields for them. For
# doing the validation, easiest to define a local function.
#
function CheckFileUpload($name, &$errors, $attributes, $value)
{
    # Note that $value will not be defined in this function.
    
    if (! isset($_FILES[$name]['name']) || $_FILES[$name]['size'] == 0) {
	$errors[$name] = "Missing required value";
	return;
    }
}
63 64 65
$fields['myfile'] = array('#type'        => 'file',
			  '#label'       => 'Your File',
			  '#size'        => 30,
66 67
			  '#checkslot'   => 'CheckFileUpload',
			  '#description' => 'A File'
68
			  );
69
$fields['MAX_FILE_SIZE'] = array('#type' => 'hidden', '#value' => "1024");
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

# A plain checkbox
$fields['slot3']  = array('#type'        => 'checkbox',
			  '#return_value'=> "Yep",
			  '#label'       => 'Check this Box',
			  );

# A list of checkboxes
$fields['slot4']  = array('#type'        => 'checkboxes',
			  '#label'       => 'Check some of these',
			  '#boxes'       =>
			    array('box1' => array('#return_value' => "Yep",
						  '#label' => 'L1'
						  ),
				  'box2' => array('#return_value' => "Yep",
						  '#label' => 'L2'
						  ),
				  'box3' => array('#return_value' => "Yep",
						  '#label' => 'L3'
						  ),
				  ),
			  );

# A radio checklist.
$fields['slot5']  = array('#type'       => 'radio',
			  '#label'      => 'Check one of these',
			  '#radios'     =>
			   array('Rad1' => array('#label' => 'R1',
						 '#return_value' => "R1v",
						 ),
				 'Rad2' => array('#label' => 'R2',
						 '#return_value' => "R2v",
						 ),
				 'Rad3' => array('#label' => 'R3',
						 '#return_value' => "R3v",
						 ),
				 ),
			  );

# A selection list.
$fields['slot6']  = array('#type'       => 'select',
			  '#label'      => 'A selection of items',
			  '#default_value' => 'B',
			  '#options'    => array('A' => 'Pick A',
						 'B' => 'Pick B',
						 'C' => 'Pick C'),
			  '#description'=> 'Bla Bla Bla'
			  );

# A hidden Field.
$fields['slot7']  = array('#type'  => 'hidden',
			  '#value' => "69",
			  );

# A simple subsection (rendered as an inner table).
$fields['address'] = array('#type'   => 'table',
			   '#label'  => 'Fill in your addresss',
			   '#fields' =>
			    array('part1' => array('#type' => 'textfield',
						   '#label' => 'Street',
						   '#size' => 60,
						   ),
				  'part2' => array('#type' => 'textfield',
						   '#label' => 'Town',
						   '#value' => 'Corvallis',
						   '#size' => 30,
						   ),
				  ),
			   );
# The submit button.				
$fields['submit']  = array('#type'  => 'submit',
			   '#value' => "Submit This Form",
			   );

144 145 146 147 148 149 150 151 152
$errors  = array();

if (isset($formfields)) {
    FormValidate($form, $errors, $fields, $formfields);
}
else {
    $formfields = null;
}

153
echo "<center>";
154
FormRender($form, $errors, $fields, $formfields);
155 156 157 158 159 160 161
echo "</center>";

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>