jrw4 Posted December 5, 2009 Share Posted December 5, 2009 I am not sure if this goes here or not but I couldn't find a more appropriate board. I find myself in PHP using a lot of forms to get input from the user which are quite annoying to write every time. Is there anything out there to generate forms for me much easier whether it be in PHP, jQuery or any other language? What would you recommend if there is such a tool? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/ Share on other sites More sharing options...
Zane Posted December 5, 2009 Share Posted December 5, 2009 Are you saying that you're tired of typing HTML? Do you want a instant form button? I don't quite understand. Perhaps use a WYSIWIG editor? Be a little more elaborative.. Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/#findComment-972088 Share on other sites More sharing options...
mrMarcus Posted December 5, 2009 Share Posted December 5, 2009 Do you want a instant form button?remember, if you give one to him, you have to give one to everybody. Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/#findComment-972089 Share on other sites More sharing options...
jrw4 Posted December 5, 2009 Author Share Posted December 5, 2009 An instant form button? Don't be ridiculous. I am thinking more on the lines of an API or some other tool where I could do something as simple as : $form = new Form(); $form->add("username", "text"); $form->output(); Something like this which would just generate a textfield called "username" that I am able to style with CSS. Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/#findComment-972091 Share on other sites More sharing options...
Zane Posted December 5, 2009 Share Posted December 5, 2009 you can always make one yourself. You seem to have the syntax down pat. Just create yourself a Form Class. That's exactly what making classes are for. At which point you can extend upon it in the future with such things as form verification. If you don't feel like doing that kind of work then search through PHPClasses.org Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/#findComment-972092 Share on other sites More sharing options...
mrMarcus Posted December 5, 2009 Share Posted December 5, 2009 PHPClasses.orgis it just me, or has that site needed a design overhaul for a long, long time now. i can't use that site because of how elementary the design is. Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/#findComment-972119 Share on other sites More sharing options...
Alex Posted December 5, 2009 Share Posted December 5, 2009 I actually made a class specifically for this when I was bored one day. I guess it turned out not to be so useful in the long run because I don't use it anymore, but I suppose that could be because I don't use too many forms, and even when I do on larger projects I work with my design partner who does all of the html anyway.. I'll do a search and see if I can find it.. Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/#findComment-972125 Share on other sites More sharing options...
jrw4 Posted December 6, 2009 Author Share Posted December 6, 2009 Nah it's fine, there's a form helper in most frameworks anyways. http://docs.kohanaphp.com/helpers/form That's the documentation on the Kohana framework form helper. It's exactly what I was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/#findComment-972156 Share on other sites More sharing options...
Alex Posted December 6, 2009 Share Posted December 6, 2009 edit: Lol, it appears you found a helper from another framework (I was gonna suggest that as well, codeigniter has one, but I checked it out and it would hard to edit to your needs). It's okay, no harm done, had nothing else to do anyway . -- I couldn't find it :-\ I decided to make a basic Form generator class anyway (didn't have anything better to do), maybe it'll something to start with. With something this simple it's hard to find a balance between making versatile enough and still making it easier than just manually writing out the entire form, but hopefully this does that for you. I'm sure there's room for improvement, it's just something that I whipped together really quick. Class: class Form { private $_form, $_xhtml, $_elements = array(); public function __construct($action, $method, $other = array(), $xhtml = true) { $this->_xhtml = $xhtml; foreach(array_merge(array('action' => $action, 'method' => $method), (array) $other) as $attrib => $val) { $attribs[] = "$attrib='$val'"; } $this->_form = "<form " . implode(' ', $attribs) . ">\n"; } public function add() { $elements = func_get_args(); $this->_elements = array_merge($this->_elements, $elements); } public function output() { $this->build(); echo $this->_form; } private function build() { foreach($this->_elements as $element) { $attribs = array(); foreach($element as $attrib => $val) { $attribs[] = "$attrib='$val' "; } $xhtml = ($this->_xhtml) ? '/' : null; $this->_form .= "<input " . implode($attribs) . $xhtml . "><br " . $xhtml . ">\n"; } $this->_form .= "</form>\n"; } } Examples: #1: Basic usage $form = new Form('', 'post'); $form->add(array( array( 'type' => 'text', 'name' => 'username', 'value' => 'default value' )) ); $form->output(); When instantiating the Form object you pass the action and the method, then you have 2 optional arguments. The first optional argument is an array that will hold additional attributes you want to apply to the form tag (the same way you list attributes for the add() method). If you want to leave this out but still pass the 4th argument simply pass null. The last optional argument defaults to true and just tells the class whether or not to use XHTML (Just changes things like <input ... > to <input ... /> and <br> to <br /> to comply with XHTML standards). output: <form action='' method='post'> <input type='text' name='username' value='default value' /><br /> </form> #2: Adding multiple elements at once: $form = new Form('', 'post'); $form->add( array( 'type' => 'text', 'name' => 'username', 'value' => 'default' ), array( 'type' => 'text', 'name' => 'password' ), array( 'type' => 'submit', 'name' => 'submit', 'value' => 'Submit!' ) ); $form->output(); Optionally if you wish to add multiple inputs at the same time you can simply by adding other parameters each containing an array to represent each element (the same way as in the first example). Output: <form action='' method='post'> <input type='text' name='username' value='default' /><br /> <input type='text' name='password' /><br /> <input type='submit' name='submit' value='Submit!' /><br /> </form> -- Now this gives you a decent amount of control, you can add attributes like classes to elements to format them using CSS, but if you want even more control like adding wrappers you can also add that functionality. Quote Link to comment https://forums.phpfreaks.com/topic/184118-form-generation/#findComment-972159 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.