Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Release] Form Generation class.

Intelligent DoucheBag
Loyal Member
Joined
Jan 5, 2008
Messages
1,698
Reaction score
288
I saw the thread of qet and he created some "form generator" codes.
it was horrible.

so I created a form generation class.




Version 1:
Usage:
PHP:
<?php
	//require the class
	require("require/Form_Generator.php");

	// First value = destination, second value = method, enctype is autoset. 
	$form = new form_generator("index.php?ThisIsTheDestination");

	// normal text boxes.
	$form->textbox("TextBoxname", "TextBox Label", "", "TextBox Placeholder");
	$form->textbox("TextBoxname", "TextBox Label", "TextBox Value", "");

	// break line
	$form->breakline("This is a break line!", "red"); 

	// password textboxes
	$form->password("password", "Password", "", "Your Password");  
	$form->password("rpassword", "Repeat Password", "", "Repeat Your Password");
	
	// break line
	$form->breakline("Check if both passwords match!", "blue");

	// create selections
	$form->select("selectionname", "Selection Label", "Choice1, Choice2, Choice3", "chc1, chc2, chc3"); 

	// create radio buttons
	$form->radio("radioname", "Radio Label", "Choice1, Choice2, Choice3", "chc1, chc2, chc3");

	// create checkboxes
	$form->checkbox("checkboxname", "Checkbox Label", "Choice1, Choice2, Choice3", "chc1, chc2, chc3");

	// break line
	$form->breakline("Wasn't this usefull?", "green");

	// file upload
	$form->file("NameofFile", "File Label");

	//submit button
	$form->submit("Register");

	// Create the form.
	$form->buildForm();
?>

Form_Generator.php class:
PHP:
<?php

	//////////////////////////////
	//							//
	//		 This class has 	//
	//		been created by 	//
	//		 Jurien Hamaker 	//
	//							//
	// http://jurienhamaker.net //
	//							//
	//		This is a free 		//
	//		   release 			//
	//							//
	//////////////////////////////

	class form_generator
	{

		var $destination;
		var $method;
		var $enctype = '';

		var $fmBody = '';
		var $fmHeader;
		var $fmFooter;
		var $form;

		var $choices;
		var $names;
		var $options = '';

		public function __construct($destination = '', $method = 'POST')
		{
			$this->destination = $destination;
			$this->method = $method;
		}

		public function submit($value = "submit", $name = "")
		{
			$this->fmBody .= '<tr><td></td><td><input type="submit" value="'.$value.'" name="'.$name.'"></td></tr>';
		}

		public function textbox($name, $label = '', $value = '', $placeholder = '', $color = NULL)
		{
			$this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td><input id="'.$name.'" type="text" name="'.$name.'" value="'.$value.'" placeholder="'.$placeholder.'"></td></tr>';
		}

		public function password($name, $label = '', $value = '', $placeholder = '', $color = NULL)
		{
			$this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td><input id="'.$name.'" type="password" value="'.$value.'" placeholder="'.$placeholder.'" name="'.$name.'"></td></tr>';
		}

		public function breakline($tekst = NULL, $color = NULL)
		{
			$this->fmBody .= '<tr><td colspan="2" style="text-decoration:underline; padding-top:5px; padding-bottom:5px;"><center><font color="'.$color.'">'.$tekst.'</font></center></td></tr>';
		}
		
		public function select($name, $label = '', $choices, $names, $color = NULL)
		{
			$choices = str_replace(" ", "", $choices);
			$names = str_replace(" ", "", $names);

			$this->choices = explode(",", $choices);
			$this->names = explode(",", $names);

			$i = 0;

			foreach($this->choices as $choice)
			{
				$this->options .= '<option value="'.$this->names[$i].'">'.$this->choices[$i].'</option>';
				$i++;
			}

			
			$this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>

				<select id="'.$name.'" name="'.$name.'">
				'.$this->options.'
				</select>

			</td></tr>';
		}

		public function radio($name, $label = '', $choices, $names, $color = NULL)
		{
			$choices = str_replace(" ", "", $choices);
			$names = str_replace(" ", "", $names);
			
			$this->choices = explode(",", $choices);
			$this->names = explode(",", $names);

			$i = 0;

			$this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>';

			foreach($this->choices as $choice)
			{
				$this->fmBody .= '<input type="radio" name="'.$name.'" value="'.$this->names[$i].'"> '.$this->choices[$i].'<br>';
				$i++;
			}
			
			$this->fmBody .= '</td></tr>';
		}

		public function checkbox($name, $label = '', $choices, $names, $color = NULL)
		{
			$choices = str_replace(" ", "", $choices);
			$names = str_replace(" ", "", $names);
			
			$this->choices = explode(",", $choices);
			$this->names = explode(",", $names);

			$i = 0;

			$this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>';

			foreach($this->choices as $choice)
			{
				$this->fmBody .= '<input type="checkbox" name="'.$name.'" value="'.$this->names[$i].'"> '.$this->choices[$i].'<br>';
				$i++;
			}
			
			$this->fmBody .= '</td></tr>';
		}

		public function file($name, $label = '')
		{
			$this->enctype = "multipart/form-data";

			$this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>
				<input id="'.$name.'" type="file" name="'.$name.'">
			</td></tr>';
		}

		public function buildForm()
		{
			$this->fmHeader = '<table><form method="'.$this->method.'" action="'.$this->destination.'" enctype="'.$this->enctype.'">';
			$this->fmFooter = '</form></table>';

			$this->form = $this->fmHeader . $this->fmBody . $this->fmFooter;

			echo $this->form;
		}


	}

	// end of class.
?>


Version 2:
Usage:
PHP:
<?php
	//require the class
	require("require/Form_Generator.php");

	// First value = destination, second value = method, enctype is autoset. 
	Form::Start("index.php?ThisIsTheDestination", "POST", true);

	// normal text boxes.
	Form::textbox("TextBoxname", "TextBox Label", "", "TextBox Placeholder");
	Form::textbox("TextBoxname", "TextBox Label", "TextBox Value", "");

	// break line
	Form::breakline("This is a break line!", "red"); 

	// password textboxes
	Form::password("password", "Password", "", "Your Password");  
	Form::password("rpassword", "Repeat Password", "", "Repeat Your Password");
	
	// break line
	Form::breakline("Check if both passwords match!", "blue");

	// create selections
	Form::select("selectionname", "Selection Label", array("Choice 1"=>"chc1", "Choice 2"=>"chc2", "Choice 3"=>"chc3")); 

	// create radio buttons
	Form::radio("radioname", "Radio Label", array("Choice 1"=>"chc1", "Choice 2"=>"chc2"));

	// create checkboxes
	Form::checkbox("checkboxname", "Checkbox Label", array("Choice 1"=>"chc1", "Choice 2"=>"chc2", "Choice 3"=>"chc3", "Choice 4"=>"chc4"));

	// break line
	Form::breakline("Wasn't this usefull?", "green");

	// file upload
	Form::file("NameofFile", "File Label");

	//submit button
	Form::submit("Register");

	// Create the form.
	Form::End();
?>

Class:
PHP:
<?php

	//////////////////////////////
	//							//
	//		 This class has 	//
	//		been created by 	//
	//		 Jurien Hamaker 	//
	//							//
	// http://jurienhamaker.net //
	//							//
	//		This is a free 		//
	//		   release 			//
	//							//
	//////////////////////////////

	class Form
	{

		var $destination;
		var $method;
		var $enctype = '';

		var $fmBody = '';
		var $fmHeader;
		var $fmFooter;
		var $form;

		var $choices;
		var $names;
		var $options = '';

		public function start($destination = '', $method = 'POST', $enctype = false)
		{
			if($enctype == true)
			{
				$enctype = 'multipart/form-data';
			}

			echo '<form method="'.$method.'" action="'.$destination.'" enctype="'.$enctype.'"><table>';
		}

		public function submit($value = "submit", $name = "")
		{
			echo '<tr><td></td><td><input type="submit" value="'.$value.'" name="'.$name.'"></td></tr>';
		}

		public function textbox($name, $label = '', $value = '', $placeholder = '', $color = NULL)
		{
			echo '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td><input id="'.$name.'" type="text" name="'.$name.'" value="'.$value.'" placeholder="'.$placeholder.'"></td></tr>';
		}

		public function password($name, $label = '', $value = '', $placeholder = '', $color = NULL)
		{
			echo '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td><input id="'.$name.'" type="password" value="'.$value.'" placeholder="'.$placeholder.'" name="'.$name.'"></td></tr>';
		}

		public function breakline($tekst = NULL, $color = NULL)
		{
			echo '<tr><td colspan="2" style="text-decoration:underline; padding-top:5px; padding-bottom:5px;"><center><font color="'.$color.'">'.$tekst.'</font></center></td></tr>';
		}
		
		public function select($sname, $label = '', $choices, $color = NULL)
		{
			
			echo '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>

				<select id="'.$sname.'" name="'.$sname.'">
				';

				foreach($choices as $name=>$value)
				{
					echo '<option value="'.$value.'">'.$name.'</option>';
				}

			echo '</select>

			</td></tr>';
		}

		public function radio($rname, $label = '', $choices, $color = NULL)
		{
			echo '<tr><td><label for="'.$rname.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>';

			foreach($choices as $name=>$value)
			{
				echo '<input type="radio" name="'.$rname.'" value="'.$value.'"> '.$name.'<br>';
			}
			
			echo '</td></tr>';
		}

		public function checkbox($cname, $label = '', $choices, $color = NULL)
		{
		
			echo '<tr><td><label for="'.$cname.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>';

			foreach($choices as $name=>$value)
			{
				echo '<input type="checkbox" name="'.$cname.'" value="'.$value.'"> '.$name.'<br>';
			}
			
			echo '</td></tr>';
		}

		public function file($name, $label = '')
		{
			echo '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>
				<input id="'.$name.'" type="file" name="'.$name.'">
			</td></tr>';
		}

		public function End()
		{
			echo '</table></form>';
		}


	}

	// end of class.
?>
 
Last edited:
Joined
Jun 2, 2012
Messages
765
Reaction score
294
Instead of doing $choices, $names why not do 1 array?

PHP:
public function select($name, $label = '', $data = array(), $color = NULL)
{
	foreach($data as $var => $value)
	{
		$this->options .= '<option value="'.$var.'">'.$this->value.'</option>';
	}
	
	$this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>

		<select id="'.$name.'" name="'.$name.'">
		'.$this->options.'
		</select>

	</td></tr>';
}

Then just:
PHP:
$form->select("Select", "Select", array(1 => "Rank 1", 2 => "Rank 2"), "")
 
Intelligent DoucheBag
Loyal Member
Joined
Jan 5, 2008
Messages
1,698
Reaction score
288
Instead of doing $choices, $names why not do 1 array?

PHP:
public function select($name, $label = '', $data = array(), $color = NULL)
{
	foreach($data as $var => $value)
	{
		$this->options .= '<option value="'.$var.'">'.$this->value.'</option>';
	}
	
	$this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>

		<select id="'.$name.'" name="'.$name.'">
		'.$this->options.'
		</select>

	</td></tr>';
}

Then just:
PHP:
$form->select("Select", "Select", array(1 => "Rank 1", 2 => "Rank 2"), "")

I know it's more decent.
Dave (he posts here frequently) told me too..

but it's less user friendly for less skilled people.
 
Back
Top