/ Published in: PHP
Expand |
Embed | Plain Text
<?php define('VALID_EMAIL', "/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i"); function displayForm($form, $function) { { // Validate form and post it foreach ($form['fields'] as $field => $options) { { // Remove all non-alphanumeric characters for the id/name { $errors[] = $field; } } } } } { //Display any errors { echo '<div class="errors">'; echo 'There was an error processing your form, please check the following fields and resubmit:'; echo '<ul>'; foreach($errors as $field) { } echo '</ul>'; echo '</div>'; } // Display the form echo '<form method="post" action="#">'; foreach ($form['fields'] as $field => $options) { // PHP will make the array key the keys index if it's not an array // Remove all non-alphanumeric characters for the id/name // Default is a standard text input { } elseif ($options['type'] == 'textarea') { } elseif ($options['type'] == 'select') { foreach ($options['items'] as $item) { } echo '</select>'; } } echo '<input type="submit" value="Send" />'; echo '</form>'; } } ?>
Comments
Subscribe to comments
You need to login to post a comment.

An example of use:
// Your function that gets the data after the form is posted function emailForm($data, $email) { foreach($data as $key => $item) { $item = str_replace(htmlentities($item), "\n", ''); $message .= wordwrap(''. $key. ''. ":". $item. "", 70); }
}
$form = array( 'escape' => true, 'fields' => array( 'First name' => array('rule' => '/^[a-zA-Z]+$/'), 'Last name' => array('rule' => '/^[a-zA-Z]+$/'), 'School/Company', 'Town', 'Country', 'Phone Number', 'Email Address' => array('rule' => VALIDEMAIL), 'Subject' => array('type' => 'select', 'items' => array('General', 'Equipment', 'Sales', 'Technical', 'Web')), 'Enquiry' => array('type' => 'textarea', 'cols' => 50, 'rows' => 10, 'rule' => VALIDNOT_EMPTY), ) );
displayForm($form, 'emailForm', array('[email protected]'));
An example of use: