Jump to content

Form generation


jrw4

Recommended Posts

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

 

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

--

 

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.