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

ParameterTypeDescription
$locationstringThe URL or path where the form will be submitted.
$attributesarray(optional) HTML attributes for the form element. Defaults to an empty array ([]).

Return Value

TypeDescription
stringThe 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() with form_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 of form_open().
  • Relative paths are automatically converted to full URLs using the BASE_URL constant.
  • All attributes are automatically HTML-escaped for security.
  • The method attribute is handled specially - it's extracted from the attributes array and placed in the proper position.