Thought I'd Share my HTML Form Generator

Discussion in 'PHP' started by chatmasta, Feb 18, 2007.

  1. #1
    If you're like me, you probably use a LOT of HTML forms in your projects.

    But isn't it annoying typing out all that HTML every time you need to make one? Not only that, but it gets in the way of your PHP. That's why I made these simple, easy to use functions. The comments should make it self explanatory, and at the bottom are practical examples.

    Enjoy!

    <?php
    // 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 . '"' : '';
        foreach($att as $k)
        {
            $k = explode('=', $k);
            $out .= ' ' . $k[0] . '="' . $k[1] . '"';
        }
        $out .= '>';
        echo $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);
            $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, Feb 18, 2007 IP
    nico_swd likes this.
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Something I'd make a class of. :)
     
    nico_swd, Feb 18, 2007 IP
  3. chatmasta

    chatmasta Peon

    Messages:
    693
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah I know, I'll probably do that later.

    EDIT:

    Actually, would there be any big advantages to that? It isn't like I need to use any objects or anything. But I'm not that experienced with OOP - if anyone wants to write up an OOP version of this that'd be cool.
     
    chatmasta, Feb 18, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    It would be more user friendly, so yes. I may do it tomorrow at work if I have some time. Thanks so far though.

    *Reps*
     
    nico_swd, Feb 18, 2007 IP
  5. chatmasta

    chatmasta Peon

    Messages:
    693
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah, as of right now its syntax is pretty annoying.
     
    chatmasta, Feb 18, 2007 IP
  6. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6
    you know guys this is really a good idea ... but why wasting your valuable time writing your own code instead of using any good framework which are havings methods and objects written for faster development and avoiding code redundancy ...pick up any tested good framework which is built on top of MVC framework .. i am impressed by www.codeignitor.com
     
    rays, Feb 22, 2007 IP
  7. Brae

    Brae Banned

    Messages:
    736
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Looking nice, good job. I love to use php forms mixed with html ^_^
     
    Brae, Feb 22, 2007 IP
  8. chatmasta

    chatmasta Peon

    Messages:
    693
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I'm not really a fan of all the unnecessary overhead frameworks create. But I did see CI a long time ago and liked it, maybe I will look at it again.
     
    chatmasta, Feb 22, 2007 IP