set_form_input_multi
The `set_form_input_multi` directive is used to read multiple values from form submissions into NGINX variables.
Description
The set_form_input_multi directive is part of the NGINX form input module and is specifically designed to handle HTTP POST and PUT requests with data encoded as application/x-www-form-urlencoded. Unlike its counterpart set_form_input, which fetches a single value for a specified variable, set_form_input_multi allows for retrieving all values associated with a specific key from the request body. This is particularly useful when working with HTML form fields that allow multiple selections, such as checkboxes or multi-select dropdowns.
The directive can be placed in the http, server, or location contexts, making it flexible in terms of where it can be configured. The usage syntax is set_form_input_multi $variable [argument];, where $variable is the name of the NGINX variable to which the values will be assigned, and the optional argument specifies which form field to read from the request. If the argument is not provided, it defaults to reading the values associated with the variable name itself.
When this directive is executed, it populates the specified NGINX variable with all matching values from the form input. This behavior allows for subsequent use of these values within NGINX configurations, with potential transformation operations such as array_join to concatenate multiple values into a single string if needed.
Config Example
location /submit {
client_max_body_size 1m;
client_body_buffer_size 1m;
set_form_input_multi $data; # Read all 'data' fields into $data
set_form_input_multi $selected_items items; # Read all fields named 'items' into $selected_items
array_join ' ' $data; # Combine values into a single string
}Ensure that the client request body size is properly configured with client_max_body_size and client_body_buffer_size to avoid truncation of input data.
Remember to use array_join if you want to convert the resulting array of values into a single string for further processing.