Advice on a basic class i've written with regards to an online form help?

Hello,

I’ve written a class which I can pass values to to produce a form with input boxes into it. Here’s a dummed down version of it:


<?php

Class FormElements {
  
  private $input_type;
  private $input_name;
	
  //* set Values
    function setInputBoxes ($input_type,$input_name) {
	  $this->input_type = $input_type;
	  $this->input_name = $input_name;
	}	
	
  //* get whole form    
    function getWholeform() {
      $output = '<form>
                     <p><input type="'.$this->input_type.'" name="'.$this->input_name.'"></p>
                 </form>';
      return $output;
    }
	
}


//*Instance
$form = new FormElements();
$form->setInputBoxes("text","username");
echo $form->getWholeform();

?>

With the HTML produced like so:


<form>
  <fieldset>
   <legend>Name of form</legend>
   <p><label for="Your Username">Username:</label><input type="text" name="username"></p>
   </fieldset>
</form>

Now this is great, but as you can see the form ONLY contains one input box. The bit i’m stuck on is I want to add a load more form element input boxes on my form via my getWholeform() method ie by adding:


$form->setInputBoxes("text","username");
$form->setInputBoxes("text","password");
$form->setInputBoxes("text","catchpa");
//etc...

So:

  1. How do I recreate my class to allow me to do this dynamically using a class?
  2. How could I change it so I could also allow for textareas and selectboxes etc…

Thanks

Chris

Wow, magic thanks. I’ll digest the code and try and digest what’s going on as i’m really keen to learn OOP.

Thanks again for all your help

I regret even mentioning the form elements as objects as it’s just confused you.

Use this as a basis for what you’re building:



<?php
	class FormElements {
		private $input_type;
		private $input_name;
		private $method;
		private $action;
		
		function __construct($method, $action) {
			$this->method = $method;
			$this->action = $action;
		}
		
		function setInputBoxes ($type, $input_name) {
			$this->input_name[$input_name]['type'] = $type;
			
			$this->input_name[$input_name]['html'] = "<input type='$type' name='$input_name' />";
		}    
	
		function setSelectBox ($name, $options) {
			$this->input_name[$name]['type'] = 'select';
			$this->input_name[$name]['options'] = $options;
			if ($options) {
				$html = "<select name='$name'>";
				foreach ($options as $k=>$v)
				{
					$html .= "<option value='$k'>$v</option>";	
				}
				$html .= "</select>";
			}
			$this->input_name[$name]['html'] = $html;
		}
		
		//* get whole form    
		function getWholeform() {
			$output = "<form method='$this->method' action='$this->action'>";
			
			if ($this->input_name) {
				foreach ($this->input_name as $k=>$v)
				{
					$output .= $v['html'];
				}
			}
	
			$output .= '</form>';
			return $output;
		}
	}
	
	//*Instance
	
	$form = new FormElements('POST', 'http://www.domain.com');
	
	$form->setInputBoxes('text', 'username');
	$form->setInputBoxes('password', 'password');
	$form->setInputBoxes('password', 'verify_password');
	$form->setSelectBoxes("state", array ('AK' => 'Alaska', 'CA' => 'California') );
	
	echo $form->getWholeform();
?>

Thanks for your help,

That’s great advice, so say for a select Box I could have something like:


function setSelectBoxes ($select_id,$select_opt1,$select_val1,$select_opt2,select_val2) {       
  $this->select_id = $select_id;       
  $this->select_opt1 = $select_opt1;  
  $this->select_val1 = $select_val1;    
  $this->select_opt2 = $select_opt2; 
  $this->select_val2 = $select_val2;
} 

The bit I don’t get is when it comes to ouputting the whole box in my method ‘getWholeForm()’

How do I tell the getWholeform() method for example I want a input box, then a select box outputted dynamically as commented I just don’t get the loop bit here you talk about and how I utilize that to achieve my goal:


//* get whole form 
function getWholeform() { 
  $output = '
  <form> 
  <!--Force my instance to Stick a input box here-->
  <!-- Force my instance to Stick a select dropdown here-->
  </form>'; 
return $output; 
} 


//Input area
$form = new FormElements(); 
$form->setInputBoxes("text","username"); 

//Select Box
$form2 = new FormElements(); 
$form2->setSelectBoxes("form","1","Car","Plane","2"); 


echo $form->getWholeform(); 

In a pure OOP design, each individual form element would be an object of its own.

Building off your existing version, what you would need is several methods, one of each form element type (or it can be one large method with some sort of a switch). Each method would return the appropriate HTML for different types of form elements.

The setInputBoxes method would be changed to store these data in an array, like so:


    function setInputBoxes ($input_type,$input_name) {

      $this->input_type[$input_name] = $input_type;
    }    


And getWholeForm would be responsible for looping through each element in $this->input_type to determine which method to call, then build out the HTML string.

Don’t forget you also need to repopulate these fields on submissions with errors, as well as validations. You’ll need several Classes to build a form right. Good luck!