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:
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.
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*
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
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.