Trongate MX
Documentation

Help Improve Our Docs

If you’ve found an error, spotted something missing, or feel a section could be clearer or better explained, we’d love to hear from you. Your feedback helps keep the documentation accurate and useful for everyone.

Please report issues or suggest improvements on GitHub. Community input is invaluable in making the docs stronger.

Not comfortable with GitHub? No problem — you can also get in touch with us directly via our contact form. We welcome all feedback.

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.

Syntax Options

The mx-vals attribute requires a JSON object where each key-value pair represents a form field and its value. Here are the different ways to use this attribute:

1. Direct JSON String

Using Trongate's form helper functions:

Pure HTML equivalent:

2. Using PHP's json_encode()

Using Trongate's form helper functions:

Pure HTML equivalent:

3. Using Predefined Arrays (Recommended)

Using Trongate's form helper functions:

Pure HTML equivalent:

Usage with Forms

The mx-vals attribute is particularly powerful when combined with form submissions. Here's a complete example:

Using Trongate's form helper functions:

Pure HTML equivalent:

When this form is submitted, it will send:

  • The visible form field (first_name).
  • The additional values defined in mx-vals (last_name and age).

Server-Side Handling

Here's how to handle the submitted data in your PHP endpoint:

Technical Details

  • Values from mx-vals are merged with regular form data before the request is sent.
  • Both visible form fields and mx-vals values are accessible using the same methods on the server.
  • In case of naming conflicts, the last value processed takes precedence.
  1. Use json_encode()
    • Prefer json_encode() over manual JSON string construction.
    • Helps prevent syntax errors and ensures proper escaping.
  2. Organize Values
    • Define mx-vals arrays separately for better code organization.
    • Makes it easier to modify values dynamically.
  3. Value Types
    • Numbers don't need quotes: "age": 30.
    • Booleans are supported: "is_active": true.
    • Strings must use double quotes: "name": "John".
  4. Security
    • Always validate mx-vals data on the server side.
    • Don't trust client-side values for critical operations.
    • Use appropriate data types and validation rules.

Common Pitfalls to Avoid

  • Don't use single quotes for JSON property names.
  • Don't use unquoted property names.
  • Don't use PHP array notation directly in the mx-vals value.
  • Don't use array syntax instead of object syntax.

Error Handling

If the JSON in mx-vals is invalid:

  • An error will be logged to the browser console.
  • The values will not be included in the request.
  • The rest of the form submission will proceed normally.

Summary

The mx-vals attribute provides a powerful way to include additional data with your HTTP requests in Trongate MX. Whether you're adding hidden parameters to a button click or combining additional data with form submissions, mx-vals offers flexibility and convenience. By using PHP's json_encode() with predefined arrays, you can maintain clean, organized code while ensuring all your required data is properly transmitted to your server endpoints.

×