Download the PHP package flsouto/htbutton without Composer
On this page you can find all versions of the php package flsouto/htbutton. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package htbutton
HtButton
Overview
This class allows you to easily create html form buttons in an object oriented way and comes with the following features:
- Setting attributes and styles with ease
- Shortcut for inline/block layout
- Checking for form submission
Notice: this class extends an abstract class called HtField
. The inherited functionality will not be covered here. So, if you find difficulty in understanding something, please refer to this documentation.
Installation
Use composer:
Usage
The below code simply creates and renders a button named "action". Notice that by default the button's label is its own name with the first letter in upper case:
The above code will produce the following output:
Specifing a label
The second parameter to the constructor accepts a label string:
The output will be:
Using the label setter
If you desire to set label after construction, use the label setter:
Outputs:
Inline/Block Layout
By default the button is rendered in block mode, meaning that it will always appear in a new line.
In order to change it so that it is rendered in the same line, you have to call the inline
method:
The output will be:
As you can see, it has simply added some css properties to the button's "style" attribute.
Calling the same method with a false
argument will return to the block layout:
Output:
Adding custom CSS
Pass an array to the àttrs
method with a style
key like in the example below:
Outputs:
Changing default value
The value that gets sent on a form submission event is by default a random string which is the same as the button's id. So, if you want to change that, you have to change the id attribute:
Checking if button has been pressed
If you nested your button inside a form, chances are you will need at some point to check if that button has been pressed so you can process the requested action:
Outputs:
Handling multiple buttons
Sometimes you may want to have more than one action available in a form. In this case you will probably need to have muliple buttons and be able to tell which one has been clicked. The following snippet shows you how to do just that:
The output will be:
Handling buttons with same name
I wouldn't recommend giving the same name to different buttons on the same form, but, since I've seen this pattern been used by many developers I decided to include an example of how one could handle that problem through this API. You basically have to give a different id/value to each button and check which one has been sent in a switch block:
The output will be: