truefusion 3 Report post Posted November 25, 2008 This is a PHP class i put together a long time ago, which others might find useful. The class itself still needs some work to make it (feel) more complete; however, whether or not i apply any modifications here is another story, even though it may be easy to implement these modifications. A Small Description Originally this script was used as a way to write the submitted information to a file but was expanded to do more. It was started to provide an easy way of generating forms without having to constantly write handlers which all pretty much did the same thing. The class allows to specify which elements are to be used for each field (though it currently only supports text input types and textareas) along with attributes with values for each element. The form "template" is merely an array following a certain format. So far the class allows the ability either to write to file or send an e-mail. The e-mail portion of it allows you to specify subject and body of the e-mail; both fields allowing variables from the post data to be inserted. If no custom body message is included, it'll default to something simple. Writing to file, however, isn't yet customizable unless you edit the code. Studying the code will show where things can be improved on. There's a lot of things that could be implemented to allow further customization, but i've only been modifying this script on a per need basis. The script needs more support for other input types. Other things include custom error messages, if these custom error messages should only be for debugging purposes, extra form verifiers (e.g. if a field should be made optional; all fields are currently mandatory), a way to send the user a message, page redirection after a successful form submittion, custom headers and footers (i.e. if there should be some HTML before and after the form), doing away with $this->exclude (if you study the code, you'll know why), etc. But as for now it fulfills pretty basic needs. The Class: $this->verify(); } } function verify(){ foreach ($this as $key => $value){ if (!in_array($key, $this->exclude)){ if ($this->{$key}[1] == "string") { if (empty($_POST[$key])){ $this->errors[] = "<i>".str_replace("_", " ", $key)."</i> is required."; } } else if ($this->{$key}[1] == "integer") { if (!is_numeric($_POST[$key])){ $this->errors[] = "<i>".ucwords(str_replace("_", " ", $key))."</i> needs to be a number."; } } } } if (!is_array($this->errors) && empty($this->errors)){ $this->doSubmit(); } else if (is_array($this->errors) && !empty($this->errors)){ $this->displayErrors(); } } function parse_email_option($field){ preg_match_all("/\\$[a-zA-Z_-]+/", $field, $strs); foreach ($strs[0] as $value){ $field = str_replace($value, $_POST[str_replace("$", "", $value)], $field); } return $field; } function displayErrors(){ foreach ($this->errors as $value){ echo " <div class=\"error\">".$value."</div> "; } } function doSubmit(){ if (is_array($this->email_address)){ # E-mail Subject if (isset($this->email_address[1])){ $subject = $this->parse_email_option($this->email_address[1]); } else { $subject = "Untitled."; } # E-mail Message if (isset($this->email_address[2])){ $message = $this->parse_email_option($this->email_address[2]); } else { $message = ""; unset($_POST['submit']); foreach ($_POST as $key => $value){ $message .= "<b>".ucwords(str_replace("_", " ", $key))."</b> linenums:0'>class form { function form(){ $this->exclude = array("registered", "fwrite", "mail", "filename", "email_address", "errors", "exclude"); $this->fwrite = FALSE; $this->mail = FALSE; } function setFormVars($variables, $filename = NULL, $email_address = NULL){ foreach ($variables as $value){ $this->$value[0] = array($value[1], $value[2]); } $this->filename = $filename; $this->email_address = $email_address; if ($_POST['submit'] == "Submit"){ $this->verify(); } } function verify(){ foreach ($this as $key => $value){ if (!in_array($key, $this->exclude)){ if ($this->{$key}[1] == "string") { if (empty($_POST[$key])){ $this->errors[] = "<i>".str_replace("_", " ", $key)."</i> is required."; } } else if ($this->{$key}[1] == "integer") { if (!is_numeric($_POST[$key])){ $this->errors[] = "<i>".ucwords(str_replace("_", " ", $key))."</i> needs to be a number."; } } } } if (!is_array($this->errors) && empty($this->errors)){ $this->doSubmit(); } else if (is_array($this->errors) && !empty($this->errors)){ $this->displayErrors(); } } function parse_email_option($field){ preg_match_all("/\\$[a-zA-Z_-]+/", $field, $strs); foreach ($strs[0] as $value){ $field = str_replace($value, $_POST[str_replace("$", "", $value)], $field); } return $field; } function displayErrors(){ foreach ($this->errors as $value){ echo " <div class=\"error\">".$value."</div> "; } } function doSubmit(){ if (is_array($this->email_address)){ # E-mail Subject if (isset($this->email_address[1])){ $subject = $this->parse_email_option($this->email_address[1]); } else { $subject = "Untitled."; } # E-mail Message if (isset($this->email_address[2])){ $message = $this->parse_email_option($this->email_address[2]); } else { $message = ""; unset($_POST['submit']); foreach ($_POST as $key => $value){ $message .= "<b>".ucwords(str_replace("_", " ", $key))."</b>: ".$_POST[$key]."<br/><br/>\n"; } } $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; if (mail($this->email_address[0], $subject, $message, $headers)){ echo "<h2 style=\"text-align: center;\">Thanks for your time!</h2>"; $this->mail = TRUE; } } if ($this->filename != NULL){ unset($_POST['submit']); $handle = fopen($this->filename, 'a'); $content = ""; $array_keys = array_keys($_POST); foreach ($_POST as $key => $value){ $content .= base64_encode($_POST[$key]); if ($key != $array_keys[count($array_keys)-1]){ $content .= "||"; } } $content .= "\n"; if(fwrite($handle, $content)){ echo "<h2 style=\"text-align: center;\">Thanks for your time!</h2>"; $this->fwrite = TRUE; } fclose($handle); } if ($this->mail || $this->fwrite){ $this->registered = TRUE; } } function doForm() { if (!$this->registered) { echo "\t<form method=\"post\" action=\"\">\n\t<table align=\"center\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\">\n"; foreach ($this as $key => $value) { if (!in_array($key, $this->exclude)) { echo "\t<tr>\n\t<td valign=\"top\">".ucwords(str_replace("_", " ", $key)).":</td>\n"; if ($this->{$key}[0][0] == "text"){ echo "\t<td>\n\t<input type=\"text\" name=\"".str_replace(" ", "_", $key)."\" value=\"".$_POST[$key]."\""; if (is_array($this->{$key}[0][1])) { foreach ($this->{$key}[0][1] as $attr => $value){ if ($attr != "name" && $attr != "value"){ echo " ".$attr."=\"".$this->{$key}[0][1][$attr]."\""; } } } echo "/>\n\t</td>\n"; } else if ($this->{$key}[0][0] == "textarea"){ echo "\t<td>\n\t<textarea name=\"".str_replace(" ", "_", $key)."\""; if (is_array($this->{$key}[0][1])) { foreach ($this->{$key}[0][1] as $attr => $value){ if ($attr != "name" && $attr != "value"){ echo " ".$attr."=\"".$this->{$key}[0][1][$attr]."\""; } } } echo ">".$_POST[$key]."</textarea>\n\t</td>\n"; } echo "\t</tr>\n"; } } echo "\t<tr>\n\t<td colspan=\"2\" align=\"center\">\n\t<input type=\"reset\" value=\"Reset\"/>\n\t<input type=\"submit\" name=\"submit\" value=\"Submit\"/>\n\t</td>\n\t</tr>\n\t</table>\n\t</form>\n"; } }} How to use it in your script: $form = new form();$form->setFormVars( array( // fields array array("first_name", array("text"), "string"), array("last_name", array("text"), "string"), array("age", array("text", array("style" => "width: 70px;")), "integer"), array("body", array("textarea", array("rows" => "15", "cols" => "60")), "string") ), NULL, // (str) path to file, if any; otherwise null array("whatever@wherever.com", 'New e-mail from $first_name $last_name', '<b>$first_name</b> of $age writes: <p>$body</p>') /* array(e-mail, subject, body) for e-mail, if any; otherwise null or leave empty. * Do note the single quotes for the subject and body: important for inserting form post data. */);$form->doForm();The variables within the subject and body of the e-mail, their names must match the field names which you provide in the fields array. Share this post Link to post Share on other sites
liod 0 Report post Posted November 25, 2008 Can we add dropdown list or button with this class? Share this post Link to post Share on other sites
truefusion 3 Report post Posted November 25, 2008 Can we add dropdown list or button with this class?Those haven't been implemented yet, so for now that kind of support would require modifying the class. But i'm not entirely sure what kind of purpose a button would serve in a form. The only thing i could think of is perhaps to call a JavaScript function, but i can't think of a function that one would normally call in a form other than perhaps submitting the form. Share this post Link to post Share on other sites