admin管理员组

文章数量:1397142

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', 'woomerce' ); ?>"><?php esc_html_e( 'Salva modifiche', 'woomerce' ); ?></button>
  <input type="hidden" name="action" value="save_account_details" />
  <input type="hidden" name="action" value="update-user_" />
</p>

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', 'woomerce' ); ?>"><?php esc_html_e( 'Salva modifiche', 'woomerce' ); ?></button>
  <input type="hidden" name="action" value="save_account_details" />
  <input type="hidden" name="action" value="update-user_" />
</p>
Share Improve this question asked Aug 16, 2022 at 11:26 SnorlaxSnorlax 2932 gold badges14 silver badges50 bronze badges 4
  • 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 Commented Aug 16, 2022 at 11:28
  • So if I try to do this is it correct ? <input type="hidden" name="action" value="save_account_details, update-user_, other_valuer, etc.." /> – Snorlax Commented Aug 16, 2022 at 11:29
  • jsfiddle/d8hfv9a3/1 from this answer: stackoverflow./a/20406735/5334486 – GrafiCode Commented Aug 16, 2022 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 Commented Aug 16, 2022 at 11:31
Add a ment  | 

3 Answers 3

Reset to default 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 plex 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.

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

Sometimes I use:

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

本文标签: