A form generator called SimpleForm - Part 1

Posted: September 15th, 2008 | Author: Mihai Bojin | Filed under: PHP, Web | Tags: , , , , |

I’ve been meaning to do something like this for a long time.

I think I can safely say that forms are a web coder’s worst nightmare. Everytime you think you covered all the aspects of a project you realize that you forgot something about the website’s forms.

You can’t really standardize forms because they come in a lot of shapes and colors. The best implementation I know of is Zend_Form, but it implies that you use the whole Zend Framework and that brings a lot of overhead to the table.

If you have a small website, or you are in need of a simple lightweight form system, stay tuned !

 

Another reason for developing this generator is a problem faced by a good friend of mine, who’s a marketer and PPC specialist.

He told me how he searched the web for a simple to use, form tutorial for a non-programmer like himself. He wasn’t able to find one but he wanted to do it himself, so he struggled for a week and created… something. Something that works but that won’t help him at his next project.

I guess there are a lot of guys out there in the same position, so I’m starting this series for the web professionals and passionates that need a simple to use Form Generator. Later on in the series I will address the seasoned web coder that maybe wants to help develop this FormGenerator into a complete form management solution.

 

I’ve created the first simple version of the FormGenerator and as you might have guessed it’s called SimpleForm.

It’s small, lightweight, secure and it can send the form results after completion to one or more email addresses or your liking.

All you have to do to make it work in your scripts is to create a configuration file like the one below. In it you’ll find all the features of the form explained. You can also download the full project files from www.MihaiBojin.com/wp-content/uploads/SimpleForm/SimpleForm-v1.00.zip

 

SimpleForm.inc.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
< ?php
require_once('SimpleForm.php');
 
/**
 * Initialize SimpleForm with parameters
 * Syntax: SimpleForm($formName, $method)
 * $formName - You only have to fill this in if you have more than 1 form on the page
 * $method - Form's method: default is 'post', you can also use 'get', but it's not recommended
 */
$form = new SimpleForm('mihai', 'post');
 
/**
 * Adds an element to the form
 * Syntax: addElement($label, $type, $required, $errorMessage)
 * $label - Label that represents the element
 * $type - Type of element; right now only input-text, input-password, textarea and input-submit
 * 		   are implemented
 * $required - Determines if field completion is required; if true you will have to set $errorMessage
 * $errorMessage - Error message to show in case field is not filled in
 */
$form->addElement('Name', 'input-text', true, 'Please complete the name field!');
$form->addElement('Email', 'input-text', true, 'Please complete the email field!');
$form->addElement('Subject', 'textarea');
$form->addElement('Send', 'input-submit');
 
/**
 * If you want to show a success message after the form is shown, set it through this method
 * Syntax: addSuccessMessage($message)
 * $message - Message that you want shown after the form is completed
 */
$form->addSuccessMessage('Congratulations! You will be contacted as soon as possible!');
 
/**
 * Runs the form generator
 * At first it will just show the form
 * If the users submits the form with the required fields uncompleted, it will reshow the form plus the error message(s)
 * If everything is okay, it will just show the success message (if filled in) or nothing
 * Syntax: runForm($returnValues)
 * $returnValues - default is false; if set to true you'll have to capture the output from the return value
 * 				   like $output = $form->runForm(true); and echo it yourself
 */
$form->runForm(false);
 
/**
 * If you need to send an email (or emails) with the form's content, do it with this method
 * Syntax: mailTo($to, $from, $subject)
 * $to - can be a form element(for example 'Email') in which case it will use the value entered by the user
 * 		 or a plain email address
 * $from - can be a form element(for example 'Email') in which case it will use the value entered by the user
 * 		   or a plain email address
 * $subject - Email's subject message
 */
$form->mailTo('office@example.com', 'Email', 'Contact form email' );

 

The example configuration file sets up a simple contact form with Name, Email and Address fields.

To show the form in a web page, just include this init file (SimpleForm.init.php) in your webpage. For example I added an index.php that shows how to include the form generator:

 

index.php

1
2
3
4
5
6
< ?php
	/**
	 * Simple way of including the form in your website
	 */
	require_once( 'SimpleForm.init.php' );
?>

 

As you can see, you just have to call the require_once feature of PHP and the generator will do it’s magic.

Some implementation details:

  1. the generator is implemented in PHP 5 it will not work on any subversion of PHP 4
  2. if you don’t use PHP on your website for anything else, you’ll have to rename the target filename from "filename.html" to "filename.php" and just include the php tags from the index.php example above
  3. I tested the script and it seems to work but that may not be the case on other servers, versions of PHP etc; if you run into any problems, drop me a comment or an email and I will help you sort things out

 

Have fun with it and please share your experience with SimpleForm version 1.00 !

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google


Leave a Reply

Clicky Web Analytics