Posting Additional Parameters
The mx-vals
attribute in Trongate MX allows you to attach additional values to your HTTP requests as if they were submitted as part of an ordinary HTML form. This feature is particularly useful for dynamically adding data to your requests without modifying the form structure.
Syntax
<element mx-vals='{
"key1": "value1",
"key2": "value2"
}'></element>
The mx-vals
attribute should contain a JSON object, where each key-value pair represents a form field and its value.
Usage
To use the mx-vals
attribute:
- Add the attribute to an element that triggers an AJAX request (e.g., a button with
mx-post
,mx-put
, ormx-patch
). - Specify the additional form fields in a JSON object format.
Example
<form mx-post="trongate_pages/add_headline"
mx-vals='{
"last_name": "Doe",
"age": 30
}'>
<input type="text" name="first_name">
<button type="submit">Submit</button>
</form>
In this example:
- The form includes an input field for
first_name
. - The
mx-vals
attribute adds additional fieldslast_name
with the value "Doe" andage
with the value 30 to the form submission.
- Security: Always follow best practices for securing your API endpoints when using
mx-vals
. For more information, see Securing Endpoints With Trongate MX. - Consistency: Ensure the keys in
mx-vals
match the expected field names on the server side. - Validation: Validate the additional data on the server side to ensure it meets your application's requirements.
Additional Information
- The
mx-vals
attribute should contain a valid JSON object. Each key-value pair represents a form field and its value. - Ensure that the JSON string is properly formatted to avoid parsing errors.
- The values specified in
mx-vals
will be added to the HTTP request along with any existing form fields. - The
mx-vals
attribute works with POST, PUT, and PATCH requests. It does not apply to GET requests.
Additional Notes
- The
mx-vals
attribute is parsed and applied to the HTTP request before it is sent. - If the JSON string in the
mx-vals
attribute is invalid, an error will be logged to the console, and the values will not be applied. - Custom values can be used to manage various aspects of form submissions, such as adding hidden fields or dynamically generated data.
By utilizing the mx-vals
attribute, you can enhance the flexibility and control of your HTTP requests in Trongate MX, making it easier to manage additional form values and improve the robustness of your web applications.