SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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:

    HTML Code:
    <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:

    PHP Code:
    $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

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code PHP:
        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!

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help,

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

    PHP Code:
    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:

    PHP Code:
    //* 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(); 

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code PHP:
     
    <?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();
    ?>

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •