0

I'm building simple form

template.php

<div class="form-group relocate">
    <label for="contact_method"><?php _e('Best Contact Method', 'jobboard') ?></label>
    <?php 
        $contact_method = get_post_meta($resume_id, 'resume_contact_method', false);
        // $contact_method = explode( ',', $contact_method );
        var_dump($contact_method);
    ?>
    <ul>
        <li class="checkbox-inline">
            <input type="checkbox" id="resume_contact_method" name="resume_contact_method[]" value="email" <?php  if($contact_method){echo (in_array('email', $contact_method)) ? 'checked="checked"' : ''; } ?>><label for="resume_contact_method_email"><?php _e( 'Email', 'jobboard' ); ?></label>
        </li>
        <li class="checkbox-inline">
            <input type="checkbox" id="resume_contact_method" name="resume_contact_method[]" value="phone" <?php  if($contact_method){echo (in_array('phone', $contact_method)) ? 'checked="checked"' : ''; } ?>><label for="resume_contact_method_phone"><?php _e( 'Phone', 'jobboard' ); ?></label>
        </li>

    </ul>
</div>

function.php

for($i=0; $i<sizeof($_POST['resume_contact_method']); $i++){
    update_post_meta( $resume_id, 'resume_contact_method', $_POST['resume_contact_method'][$i] );   
}

My code issue, if i try to save data checkbox, form input just saving last data clicked. Can anyone tell me where have I done a mistake ?

Who Am I
  • 43
  • 1
  • 6
  • check this: http://stackoverflow.com/questions/3626883/what-is-the-purpose-of-the-name-attribute-in-a-checkbox-input-element – Mayank Pandeyz Sep 01 '16 at 06:08
  • 1. You can't have the same ID on multiple elements... ID's are unique per actual element. 2. Change the name to `name="resume_contact_id"` (removing the `[]`, not needed on checkboxes in the way you are using it). – M. Eriksson Sep 01 '16 at 06:08
  • @MagnusEriksson It still save just the last clicked, can't save multiple data – Who Am I Sep 01 '16 at 06:21
  • You are looping through the result and replacing the value in "resume_contact_method"-meta field with the new result in each iteration, thus only the last checkbox value gets saved. Remove your loop and just save `$_POST['resume_contact_method']` and it should be saved as an array instead. – M. Eriksson Sep 01 '16 at 06:26

1 Answers1

0

Instead of using update_post_meta. Use the delete_post_meta and add_post_meta.

if (!empty($_POST['resume_contact_method']) && is_array($_POST['resume_contact_method'])) {
    delete_post_meta($resume_id, 'resume_contact_method');
    foreach ($_POST['resume_contact_method'] as $resume_contact_method_kv) {
        add_post_meta($resume_id, 'resume_contact_method', $resume_contact_method_kv);
    }
}

Refer here working example with more details about storing multiple values under the single meta key recommended way :

https://wordpress.stackexchange.com/questions/10821/how-to-store-multiple-input-values-with-same-meta-key

Community
  • 1
  • 1
Senthil
  • 2,156
  • 1
  • 14
  • 19
  • Wouldn't this give him the same results as using `update_post_meta($resume_id, $_POST['resume_contact_method'])`, but with, in this case, using three DB-queries (one delete and two insert) instead of one (update)? – M. Eriksson Sep 01 '16 at 06:35
  • You mean `update_post_meta($resume_id, $_POST['resume_contact_method'])` works better than delete and add? – M. Eriksson Sep 01 '16 at 06:43