- A login form
- A contact form
- A registration form
- A reservation form
- Dependencies
- File upload
- More rules
|
Basic image upload
We're checking for file types
-
try clicking on the submit button without filling the form and then, as you fill the form, to see the
JavaScript validation in action
- for each example, notice how the PHP code of the form remains basically unchanged, despite the template variations
-
disable JavaScript to see the server-side validation in action
-
although in all my examples I use HTML output, you can switch to XHTML output by using the
doctye method
-
try the example in another browser and see that it works, out of the box. including IE6!
<?php
require 'path/to/Zebra_Form.php';
$form = new Zebra_Form('form');
$form->add('label', 'label_file', 'file', 'Upload an image');
$obj = $form->add('file', 'file');
$obj->set_rule(array(
'required' => array('error', 'An image is required!'),
'upload' => array('tmp', ZEBRA_FORM_UPLOAD_RANDOM_NAMES, 'error', 'Could not upload file!<br>Check that the "tmp" folder exists inside the "examples" folder and that it is writable'),
'image' => array('error', 'File must be a jpg, png or gif image!'),
'filesize' => array(102400, 'error', 'File size must not exceed 100Kb!'),
));
$form->add('note', 'note_file', 'file', 'File must have the .jpg, .jpeg, png or .gif extension, and no more than 100Kb!');
$form->add('submit', 'btnsubmit', 'Submit');
if ($form->validate()) {
print_r('<pre>');
print_r($form->file_upload);
die();
}
$form->render();
?>
In this example, the output is automatically generated by Zebra_Form's render method.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Zebra_Form Example</title>
<link rel="stylesheet" href="path/to/zebra_form.css">
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="path/to/jquery-1.9.1.min.js"><\/script>')</script>
<script src="path/to/zebra_form.js"></script>
</body>
</html>
|