The simplest way to create valid AJAX forms for your website.
This is the code used in the example form. As you can see it only took 16 lines of simple PHP code to make the whole form.
<?php
require_once ('form_producer/form_producer.php');
add_fieldset('field1', 'Example Form');
add_form_element('field1', 'Name', 'text', '', true);
add_form_element('field1', 'Email', 'text', '', true);
add_form_element('field1', 'Website', 'text', 'http://');
add_form_element('field1', 'Message', 'textarea', '', true);
add_fieldset('field2', 'Further Information');
add_text('field2', 'This is some kind of question?');
$radio_options = array('Value 1', 'Value 2', 'Value 3');
add_form_element('field2', 'Radio', 'radio', $radio_options);
add_text('field2', 'And Another kind of question?');
$check_options = array('Value 1', 'Value 2', 'Value 3');
add_form_element('field2', 'Checkbox', 'checkbox', $check_options);
$select_options = array('Value 1', 'Value 2', 'Value 3');
add_form_element('field2', 'Drop Down', 'select', $select_options);
?>
<html>
<head>
<title>Website Title</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<div id="wrapper">
<h2>PHP AJAX Form Producer</h2>
<p>The simplest way to create AJAX forms for your website.</p>
<?php generate_form(); ?>
</div>
</body>
</html>
Once you have downloaded the PHP AJAX Form Producer you need to drop the "form_producer" folder somewhere on your server.
Once you have done this open up the page you want to add the Form Producer to and add the following PHP to the top of the page:
<?php
require_once ('url/to/form_producer/form_producer.php');
$form_prod_url = 'url/to/form_producer';
//Create Your Form Here
?>
Make sure you set the $form_prod_url to the location of the "form_producer" folder relative to the file you are editing.
Finally you need to include jQuery in your HTML (usually somewhere in your <head> tags).
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
Note: Here we are including the hosted version of jQuery via Google API's. If you do it this way you don't need to download jQuery.
As you can see in the code it is really easy to create your forms. There are three functions you can use to create your forms:
add_fieldset($id, [ $legend ])
This function creates a fieldset in your form and allows you to group form elements. It requires you specify an id and you also have the option to define the legend (which defualts to "").
add_form_element($fieldset, $name, [ $type, $value, $required ])
This function creates a form element. It requires you specify a fieldset id (see above) and a name for your form element. The other variables in this function are as follows:
add_text($fieldset, $value)
This function allows you to insert normal text at any point in your form. It requires you set a fieldset id (see above) and a value for your text.
There is one final function you need to use to output your form:
<?php generate_form(); ?>
You must put this where you want to output your form. You can only create one form per page so this can only be called once and must called after you have used the above functions to create your form. See the exmaple code to get a better understanding of where things need to be placed.
In this example we are simply emailing the results of the form using the email_processor.php file in the "form_producer" folder. However you might want to create a custom processor to process the data in other ways. For example you might want to insert your form data into a MySQL database.
To create a custom form processor you need to create a new file in the "form_producer" folder (e.g. database_processor.php). You then need to set the following config value to make sure the AJAX call is sent to the right processor:
<?php
require_once ('url/to/form_producer/form_producer.php');
$form_prod_url = 'url/to/form_producer';
$form_processor = 'database_processor.php';
//Create Your Form Here
?>
Now the AJAX post will be sent to the database_processor.php file. You can then use the $_POST PHP array to retrieve your values in this file. See the email_processor.php
file for an example.