Location: PHPKode > scripts > HTML form generator and submit > form.class.php
<?php
	class Form {
		private $storage_txt = "storage.txt";
		private $components = array();
		private $obl = array();
		private $df_values = array();
		private $form_action="#";
		private $form_method="POST";
		private $form_id = "gen_form";
		private $form_class;	//CSS pointer
		private $errors = array(
			"no_fields" => "required textfields",
			"fill_fields" => "please, fill the required fields",
			"no_txt_file" => "unexisting storage file"
		);
		
		public function set_storage_file ($file) {
			$this->storage_txt = $file;
		}
		public function set_action ($action="#") {
			$this->form_action = $action;
		}
		public function set_method ($method="POST") {	// POST or GET
			$this->form_method = strtolower((string)$method);
		}
		public function set_id ($id="gen_form") {
			$this->form_id = (string)$id;
		}
		public function set_class ($class="") {
			$this->form_class = (string)$class;
		}
		public function setOptions($input="*textfield:name:fill+with+name;*textfield:email:fill+with+email;*textarea:comment") {
			$exp = explode(";", $input);
			$total_exp = count($exp);
			for ($i = 0; $i < $total_exp; $i++) {
				$exp2 = explode(":", $exp[$i]);
				$this->components[$i] = str_replace("*", "",$exp[$i]);
				((string)$exp2[0][0] == '*') ? $this->obl[$i] = true : $this->obl[$i] = false;
				if ((int)count($exp2) == 3)
					$this->df_values[$i] = (string)$exp2[2];
				else
					$this->df_values[$i] = "_";
			}
		}
		public function retrieve_html() {
			$has_class = "";
			if (!empty($this->form_class))
				$has_class = "class=\"".$this->form_class."\"";
			print("<form id=\"".$this->form_id."\" ".$has_class." method=\"".$this->form_method."\" action=\"".$this->form_action."\">\n");
			$t_inputs = count($this->components);
			for ($i = 0; $i < (int)$t_inputs; $i++) {
				$exp = explode(":", $this->components[$i]);
				$expldd_1 = $exp[0];
				$expldd_2 = $exp[1];
				$necess = $this->obl[$i];
				
				if($necess)
					print("(*)");
				switch ($expldd_1) {
					case "textfield":
						print($expldd_2);
						if (count($exp) != 3)
							print("<input type=\"text\" name=\"".$expldd_2."\" />");
						else
							print("<input type=\"text\" name=\"".$expldd_2."\" value=\"".str_replace('+', ' ', $exp[2])."\" />");
						print("\n");
						print("<br />\n");
						break;
					case "textarea":
						print($expldd_2);
						if (count($exp) != 3)
							print("<textarea name=\"".$expldd_2."\"></textarea>");
						else
							print("<textarea name=\"".$expldd_2."\">".str_replace('+', ' ', $exp[2])."</textarea>");
						print("\n");
						print("<br />\n");
						break;
					case "hidden":
						print("<input type=\"hidden\" name=\"".$expldd_2."\" value=\"".str_replace('+', ' ', $exp[2])."\" />\n");
				}
			}
			print("<input type=\"submit\" name=\"submit_form\" value=\"Submit\">\n");
			print("</form>");
		}
		public function code_processor() {
			$method = $this->form_method;
			$storage_msg = "";
			$successfuly = true;
			($method == "post") ? $mt = $_POST : $mt = $_GET;
			$count = 0;
			foreach($mt as $vals) {
				$exp = explode(":", $this->components[$count]);
				if (!empty($vals)) {
					if ($vals != "Submit")
						$storage_msg .= $exp[1].":".str_replace(array(" ", "\n", "@", ";"), array("+", "[endl]", "[at]", ""), $vals).";";
				}
				if ($this->obl[$count] == true) {
					if ($vals == "") {
						print($this->errors["fill_fields"]);
						$successfuly = false;
						break;
					}
				}
				++$count;
			}
			if ($successfuly) {
				// debug: echo $storage_msg;
				if (!file_exists($this->storage_txt)) {
					print $this->errors[2];
				} else {
					$file = file_get_contents($this->storage_txt);
					$open = fopen("storage.txt", "a");
					fwrite($open, $storage_msg."\n");
					fclose($open);
				}
			}
		}
	}
?>
Return current item: HTML form generator and submit