Posted By

Keef on 04/06/09


Tagged


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

jfherring
rombob
nkrstevski
vali29
BrianCoyDesign
satsuma
cokacola
eludnasud
rmethod
ringo380


Form generator


 / Published in: PHP
 

  1. <?php
  2. define('VALID_NOT_EMPTY', '/.+/');
  3. 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");
  4. define('ALPHANUMERIC', '/[^a-zA-Z0-9]/');
  5.  
  6. function displayForm($form, $function) {
  7. $errors = array();
  8.  
  9. if (!empty($_POST))
  10. {
  11. // Validate form and post it
  12. foreach ($form['fields'] as $field => $options)
  13. {
  14. if (is_array($options) && !empty($options['rule']))
  15. {
  16. // Remove all non-alphanumeric characters for the id/name
  17. $name = preg_replace(ALPHANUMERIC, null, strtolower($field));
  18.  
  19. if (!preg_match($options['rule'], $_POST[$name]))
  20. {
  21. $errors[] = $field;
  22. }
  23. }
  24. }
  25.  
  26. if (empty($errors)) {
  27. call_user_func($function, $_POST);
  28. }
  29. }
  30.  
  31. if (!empty($errors) || empty($_POST))
  32. {
  33. //Display any errors
  34. if (!empty($errors))
  35. {
  36. echo '<div class="errors">';
  37. echo 'There was an error processing your form, please check the following fields and resubmit:';
  38.  
  39. echo '<ul>';
  40. foreach($errors as $field)
  41. {
  42. echo sprintf('<li>%s</li>', $field);
  43. }
  44. echo '</ul>';
  45. echo '</div>';
  46. }
  47.  
  48. // Display the form
  49. echo '<form method="post" action="#">';
  50.  
  51. foreach ($form['fields'] as $field => $options)
  52. {
  53. // PHP will make the array key the keys index if it's not an array
  54. $name = is_array($options) ? $field : $options;
  55.  
  56. // Remove all non-alphanumeric characters for the id/name
  57. $form_name = preg_replace(ALPHANUMERIC, null, strtolower($name));
  58.  
  59. if ($form['escape'] == true) $name = htmlspecialchars($name);
  60.  
  61. echo sprintf('<label for="%s">%s: </label>', $form_name, $name);
  62.  
  63. // Default is a standard text input
  64. if (!is_array($options) || !isset($options['type']) || $options['type'] == 'text')
  65. {
  66. echo sprintf('<input type="text" id="%s" name="%s" value="%s" />', $form_name, $form_name, $_POST[$form_name]);
  67. } elseif ($options['type'] == 'textarea') {
  68. echo sprintf('<textarea id="%s" name="%s" cols="%s" rows="%s">%s</textarea>', $form_name, $form_name, $options['cols'], $options['rows'], $_POST[$form_name]);
  69. } elseif ($options['type'] == 'select') {
  70. echo sprintf('<select id="%s" name="%s">', $form_name, $form_name);
  71. foreach ($options['items'] as $item)
  72. {
  73. if ($form['escape'] == true) $item = htmlspecialchars($item);
  74.  
  75. echo sprintf('<option value="%s">%s</option>', $item, $item);
  76. }
  77. echo '</select>';
  78. }
  79. echo '<br />'. "\n";
  80. }
  81.  
  82. echo '<input type="submit" value="Send" />';
  83. echo '</form>';
  84. }
  85. }
  86. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Keef on April 13, 2009

An example of use:

Posted By: Keef on April 13, 2009

// 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); }

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($email, 'test', $message, $headers);

}

$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]'));

Posted By: Keef on April 13, 2009

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('<b>'. $key. '</b>'. ":". $item. "", 70);
    }

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    mail($email, 'test', $message, $headers);
}


$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' => VALID_EMAIL),
                'Subject' => array('type' => 'select', 'items' => array('General', 'Equipment', 'Sales', 'Technical', 'Web')),
                'Enquiry' => array('type' => 'textarea', 'cols' => 50, 'rows' => 10, 'rule' => VALID_NOT_EMPTY),
                )
            );

displayForm($form, 'emailForm', array('[email protected]'));

You need to login to post a comment.