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 sendaction
as an array with the values from the different inputs. If you just havename="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
3 Answers
Reset to default 2If 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:
- You can put any string you want in there
- 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="[{"id":"1"}]">
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}" />
本文标签:
版权声明:本文标题:javascript - How can I enter multiple values for the html attribute value = "val1, val2"? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744096945a2590453.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论