form_open()
function form_open(string $location, array $attributes = []): string
Description
Generates the opening tag for an HTML form. This is typically the first function called when building a form in Trongate.
Parameters
| Parameter | Type | Description |
|---|---|---|
| $location | string | The URL or path where the form will be submitted. |
| $attributes | array | (optional) HTML attributes for the form element. Defaults to an empty array ([]). |
Return Value
| Type | Description |
|---|---|
| string | The HTML opening tag for the form. |
Example #1: Basic Form
Example #2: Form with GET Method
Example #3: Form with Automatic Error Highlighting
Example #4: Trongate MX Form
Regarding Trongate MX: When using MX attributes like mx-post, the form submits asynchronously without page reloads. The mx-post attribute takes precedence over the action attribute.
Additional guidance on how to work with Trongate MX is available from the Trongate MX Documentation.
URL Handling
The $location parameter supports different URL formats:
- Always pair
form_open()withform_close()to ensure proper form structure and CSRF protection. - The default method is
post. Specify['method' => 'get']for GET forms. - For file uploads, use
form_open_upload()instead ofform_open(). - Relative paths are automatically converted to full URLs using the
BASE_URLconstant. - All attributes are automatically HTML-escaped for security.
- The
methodattribute is handled specially - it's extracted from the attributes array and placed in the proper position.