form_button()
function form_button(string $name, ?string $value = null, array $attributes = []): string
Description
Generates an HTML button element with type="button". These buttons require JavaScript for interactivity and do not submit forms.
Parameters
| Parameter | Type | Description |
|---|---|---|
| $name | string | The name attribute for the button element. |
| $value | string|null | (optional) The button's visible text. Defaults to "Submit". |
| $attributes | array | (optional) HTML attributes for the button element. Defaults to an empty array ([]). |
Return Value
| Type | Description |
|---|---|
| string | An HTML button element with type="button". |
Example #1: Basic Button
PHP
echo form_button('action-btn');
// Output:
// <button type="button" name="action-btn">Submit</button>Example #2: JavaScript Action Button
PHP
$attributes = [
'onclick' => 'showDetails()'
];
echo form_button('details-btn', 'View Details', $attributes);
// Output:
// <button type="button" name="details-btn" onclick="showDetails()">View Details</button>Example #3: Reset Button for Forms
PHP
$attributes = [
'type' => 'reset'
];
echo form_button('reset-btn', 'Clear Form', $attributes);
// Output:
// <button type="reset" name="reset-btn">Clear Form</button>Example #4: Button with Data Attributes
PHP
$attributes = [
'data-id' => '123',
'data-action' => 'delete',
'onclick' => 'confirmAction(this)'
];
echo form_button('item-action', 'Delete Item', $attributes);
// Output:
// <button type="button" name="item-action" data-id="123" data-action="delete" onclick="confirmAction(this)">Delete Item</button>Button Styling
Available CSS Classes
Your buttons may have different colors than those displayed above. The actual appearance depends on your Trongate CSS theme.
Try It Yourself
Test different CSS classes with your buttons. This code assumes Trongate CSS is loaded on your webpage:
View File
<?php
echo form_button('btn1', 'Default Button');
echo form_button('btn2', 'Success Button', ['class' => 'success']);
echo form_button('btn3', 'Danger Button', ['class' => 'danger']);
echo form_button('btn4', 'Warning Button', ['class' => 'warning']);
echo form_button('btn5', 'Inverse Button', ['class' => 'inverse']);
echo form_button('btn6', 'Alt Button', ['class' => 'alt']);
?>Button vs Submit: This creates <button type="button"> elements. For form submission, use form_submit() instead.
- Type="button" buttons require JavaScript for functionality
- Can be used as reset buttons with
type="reset"attribute - Use data attributes (
data-*) to store custom information - Available CSS classes:
success,danger,warning,inverse,alt - Buttons are not included in form submission data
Common Use Cases
- Modal triggers and dialog openers
- Form reset/clear functionality
- JavaScript-powered actions (show/hide, filtering, sorting)
- Cancel buttons that navigate away without submitting
- Interactive elements that trigger AJAX requests