PHP Forms - Code by hand or use tool? Which tool?

Discussion in 'PHP' started by andheresjohnny, Dec 10, 2006.

  1. #1
    I'm starting a new project where I am going to be using more PHP forms/fields/processing.

    Do most folks here code PHP forms by hand (as I do), or do you use some tool or CMS that makes this task easier? Any suggestions?

    Thanks.
     
    andheresjohnny, Dec 10, 2006 IP
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #2
    I do it by hand because sometimes generators can be generally confusing - if you have 0 knowledge making forms you can google and find a script and modify it because they are really easy to understand.
     
    crazyryan, Dec 10, 2006 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Hand, generators are good if you don't know what your up to, but they can never get it exactly right, the author has never thought of everything, however, that's not to say its not a good idea to automate the process, you could make templates of different form elements and call them with php instead of the whole tedius business of writing every element.....something like

    
    <?
    class forms
    {
    	var $html;
    	
    	function start( $method, $extra, $action = null, $name = null )
    	{
    		if($name)
    		{
    			$namet = "name = $name ";
    		}
    		$this->html .= "<form action=\"$action\" method=\"$method\" $namet $extra>\n";
    	}
    	function input_text($name, $value, $id = null)
    	{
    	 	if(!$id)
    	 	{
    			$idt = "id = $id";
    		}
    		$this->html .= "<input type=\"text\" name=\"$name\" $idt />\n";
    	}
    	function submit($value, $id = null, $name = null)
    	{
    		$this->html .= "<input type=\"submit\" value=\"$value\">\n";	
    	}
    	function select($array, $mode = null, $id = null)
    	{
    	 	if ($array)
    	 	{
    			$this->html .= "<select name=\"$name\" id=\"$id\">\n";
    		}
    		foreach ($array as $key => $value)
    		{
    			$this->html .= "<option name=\"$key\" value=\"$value\">$value</option>\n";
    		}	
    	}
    	function end()
    	{
    		$this->html.= "</form>\n";
    		echo $this->html;
    	}
    }
    
    $array = array
    (
    "First Select Value" => "First Select Text",
    "Second Select Value" => "Second Select Text"
    );
    
    $form = &new forms;
    $form->start("post", "onclick='javascript: whatever();'");
    $form->input_text("nameof", "valueof");
    $form->select($array);
    $form->submit("Submit");
    $form->end();
    
    PHP:
    It depends how you like to work I spose, I have never used anything like that before, but if I were presented with an overwhelming amount of forms to write I most probably would write something like that
     
    krakjoe, Dec 10, 2006 IP
  4. nightmaster

    nightmaster Peon

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have a forum by hand and I think that it might be better using a prepared one.
    Though to make it in the first place took me 3 days, I still have many features missing and have spent a lot of time since them on various features that you are probably not aware of now.
    Those include:
    1. making it SEO friendly.
    2. tracking users.
    3. blocking certain texts and or/users.
    4. uploading images.
    5. adding icons.
    6. quoting from other messages.
    ...

    So the basic stuff is fairly easy, but there are tons of features that will take forever to code.
    Never tried using a prepared forum, but played a bit with phpbb, which looks good. Only problem is that if you need to change it - it will take long time and I can easily perform changes on my code.
     
    nightmaster, Mar 2, 2007 IP
  5. chatmasta

    chatmasta Peon

    Messages:
    693
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here's the functions I'm using in my current project. I will probably turn it into a class at some point, unless someone else wants to do that.

    <?php
    /***************************************************************************
      * includes/functions/form_gen.php - HTML form generation functions      **
      * Copyright (C) 2007 Miles Richardson                                   **
      * created 2/18/07 by miles                                              **
                                                                              **
      ----------- TODO -----------                                            **
        * possibly turn it into a class?                                      **
        * maybe improve select box functionality                              **
    ****************************************************************************/
    
    
    // openForm - opens a form
       # $action: form action (a file)
       # $method: form method, post|get
       # $name: name of form
       # $att: defines additional attributes, takes an array. e.h. array('target=_blank', 'id=formid')
    function openForm($action, $method, $name, $att)
    {
    	$out = '<form';
    	$out .= (!empty($action)) ? ' action="' . $action . '"' : '';
    	$out .= (!empty($method)) ? ' method="' . $method . '"' : '';
    	$out .= (!empty($name)) ? ' name="' . $name . '"' : '';
    	if(is_array($att))
    	{
    		foreach($att as $k)
    		{
    			$k = explode('=', $k);
    			if( (!empty($k[0])) && (!empty($k[1])) )
    			{
    				$out .= ' ' . $k[0] . '="' . $k[1] . '"';
    			}
    		}
    	}
    	$out .= '>';
    	return $out;
    }
    
    // closeForm - closes a form
    function closeForm()
    {
    	echo '</form>';
    }
    
    // genField - generates a form field
       # $f_type: input|textarea|label|fieldset|legend|select|optgroup|option|button
       # $att: defines attributes, takes an array. e.g. array('value=foo', 'name=bar')
       # $wrap: if applicable (all except button, input, option) what to wrap around (most often the value)
    function genField($f_type, $att, $wrap)
    {
    	$out = '<' . $f_type;
    	
    	foreach($att as $k)
    	{
    		$k = explode('=', $k);
    		if( (!empty($k[0])) && (!empty($k[1])) )
    		{
    			$out .= ' ' . $k[0] . '="' . $k[1] . '"';
    		}
    	}
    	if($f_type == 'input' | 'button')
    	{
    		$out .= ' />';
    	}
    	else {
    		$out .= '>';
    		if(!empty($wrap))
    		{
    			if(is_array($wrap))
    			{
    				foreach($wrap as $k)
    				{
    					$out .= $k . '';
    				}
    			}
    			else {
    				$out .= $wrap;
    			}
    		}
    		$out .= '</' . $f_type . '>';
    	}
    	return $out;
    }
    
    // open form
    openForm($_SERVER['PHP_SELF'], 'post', 'form_name', array('id=form_id', 'target=_blank'));
    
    // text input field, value username, name username
    $att = array('type=text', 'value=username', 'name=username');
    echo genField('input', $att, '');
    
    echo '<br /><br />';
    
    // textarea with 55 columns, 4 rows, value Hello World
    $att = array('cols=55', 'rows=4');
    echo genField('textarea', $att, '');
    
    echo '<br /><br />';
    
    // option set
    $options = array(
    				genField('option', array('value=o1'), 'Option 1'), 
    				genField('option', array('value=o2', 'selected=selected'), 'Option 2'), 
    				genField('option', array('value=o3'), 'Option 3')
    				);
    
    $att = array('name=select_box');
    echo genField('select', $att, $options);
    
    // close form
    closeForm();
    
    ?>
    PHP:
     
    chatmasta, Mar 3, 2007 IP
  6. WebGeek182

    WebGeek182 Active Member

    Messages:
    510
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    95
    #6
    WebGeek182, Mar 3, 2007 IP
  7. nightmaster

    nightmaster Peon

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Oh, I must have been really tired when writing the above message.
    I thought you are referring to forums not forms. Form can be done by hand for sure.
     
    nightmaster, Mar 3, 2007 IP
  8. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    How about the validation part? I found a form class that includes validation but I haven't ran into others so I use this one but i'd like to know if there are other options out there to do the validation.
     
    Choller, Mar 7, 2007 IP
  9. steb

    steb Peon

    Messages:
    213
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    if its going to get complex, ill sometimes do a mock up in a wysiwyg editor then strip out all the unneeded bits in notepad2 and go from there..
     
    steb, Mar 7, 2007 IP