I have a form that needs to save on multiple input fields. So I was wondering if I can somehow put all the values into a single attribute. Is this possible ? Maybe even with Js solutions …
<p>
<?php wp_nonce_field( 'save_account_details', 'save-account-details-nonce', 'update-user_' . $user->ID ); ?>
<button type="submit" class="edit-account-button" name="save_account_details" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>"><?php esc_html_e( 'Salva modifiche', 'woocommerce' ); ?></button>
<input type="hidden" name="action" value="save_account_details" />
<input type="hidden" name="action" value="update-user_" />
</p>
3
Answers
You can have a string. The spec doesn’t define any particular format for the data in the attribute.
You can get values by spliting
your_data_attribute_value.split(",");
If all you’re looking for is an array of values then you can separate them into multiple inputs with array-specified names. For example:
When posted to the server
$_POST['test']
would be an array with these three values.If you want something more complex that is also possible, but perhaps not in the way you’re thinking. The value of any given
<input>
(or<button>
, etc.) is a string. Which means:That string can contain delimeted values:
It can contain JSON (which would have to be HTML-encoded):
It can contain any string you like.
What you do with that string is what matters. For example, if a delimeted string is posted to the server then you can
explode()
that string into its values. Or if a JSON string is posted to the server then you canjson_decode()
that string into its data structure. (You may need tohtml_entity_decode()
it first, that’s worth testing.)It’s a string like any other, you can parse it like any other.
Sometimes I use: