0

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>
Snorlax
  • 183
  • 4
  • 27
  • 1
    You can put whatever value you like in your input. That value, in its entirety, will be a string. You can parse that string any way you like when you use it. – David Aug 16 '22 at 11:28
  • So if I try to do this is it correct ? `` – Snorlax Aug 16 '22 at 11:29
  • http://jsfiddle.net/d8hfv9a3/1/ from this answer: https://stackoverflow.com/a/20406735/5334486 – GrafiCode Aug 16 '22 at 11:31
  • If you want to have multiple input fields with the same name, you need to add a `[]` after the name: `name="action[]"`. Then it will send `action` as an array with the values from the different inputs. If you just have `name="action"`, then only one will be sent (and that value can contain what ever you want). – M. Eriksson Aug 16 '22 at 11:31

3 Answers3

2

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(",");

  • 1
    This is valid as well and _sometimes_ has rights to exist. But usually you'd wanna do it the proper way, like David explained above – Honk der Hase Aug 16 '22 at 11:53
2

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:

<input name="test[]" value="1">
<input name="test[]" value="2">
<input name="test[]" value="3">

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:

  1. You can put any string you want in there
  2. The code sees it as a string, just like any other string

That string can contain delimeted values:

<input value="one,two,three">

It can contain JSON (which would have to be HTML-encoded):

<input value="[{&quot;id&quot;:&quot;1&quot;}]">

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 can json_decode() that string into its data structure. (You may need to html_entity_decode() it first, that's worth testing.)

It's a string like any other, you can parse it like any other.

David
  • 208,112
  • 36
  • 198
  • 279
  • Thanks for your answer, I think I understand but I'm not completely sure how to do it. If you like could you kindly leave an example based on the code I left in the question ? – Snorlax Aug 16 '22 at 11:46
  • @Snorlax: What have you tried and what isn't working as expected? What do you emit to the browser as the value? In the browser's page source, what is the resulting HTML of the element? When it's posted to the server, how are you handling that string value? When you try, what doesn't work? – David Aug 16 '22 at 11:49
  • What I posted in the original question works. My goal was to delete this ``, and add its value to this ` ` So instead of having two inputs I would have only one with multiple values, like this for example: `` I tried to do exactly that, but on saving it just reloads the page without making any changes. – Snorlax Aug 16 '22 at 11:56
  • 1
    @Snorlax: For *just those two* `` elements, the easiest approach is to keep both of them and rename them to `name="action[]"`. Then in the PHP code `$_POST['action']` will be an array with the two values. As for "reloading the page without making changes", that's a separate problem entirely. We don't know what your server-side code does or what "changes" you are expecting to be made. All you're asking about is how to post input values to the server. – David Aug 16 '22 at 11:59
1

Sometimes I use:

$val = array();
$val["key"] = "example1";
$json = json_encode($val);
$b66 = base64_encode($json);
                        
<input type="text" name="ee..." value="{$b66}" />
Blazej Kita
  • 99
  • 1
  • 1
  • 10