skip to Main Content

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


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

    Login or Signup to reply.
  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.

    Login or Signup to reply.
  3. Sometimes I use:

    $val = array();
    $val["key"] = "example1";
    $json = json_encode($val);
    $b66 = base64_encode($json);
                            
    <input type="text" name="ee..." value="{$b66}" />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search