1

I am trying to save the checkbox fields in an array into a user meta key. With the below code I am getting the error below:

Notice: Undefined offset: 1 in xyz path on line 54 value="">

Notice:Undefined offset: 2 in xyz path on line 54 value="">

Notice: Undefined offset: 3 in xyz path on line 54 value="">

Notice: Undefined offset: 4 in xyz path on line 54 value="">

Notice: Undefined offset: 5 in xyz path on line 54 value="">

Notice: Undefined offset: 6 in xyz path on line 54 value="">

$dps_is_store_closed = get_user_meta($user_id, '_dps_is_store_closed', true);
$daysweek2 = array(
'0' => 'Monday',
'1' => 'Tuesday',
'2' => 'Wednesday',
'3' => 'Thursday',
'4' => 'Friday',
'5' => 'Saturday',
'6' => 'Sunday',
);
<table border="0">
  <tr>
     <th>Closed for the Day</th>
  </tr>
 <tr>
   foreach($daysweek2 as $key => $value){
    <td>
      <input type="checkbox" id="dps_is_store_closed[<?php $key?>]" name="dps_is_store_closed[<?php $key?>]" <?php checked( $dps_is_store_closed[$key], 'on' ); ?> value="">    
    </td>
   }
</tr>

4
  • 1
    change <input type="checkbox" id="dps_is_store_closed[<?php $key?>]" name="dps_is_store_closed[<?php $key?>]" <?php checked( $dps_is_store_closed[$key], 'on' ); ?> value=""> to this <input type="checkbox" id="dps_is_store_closed[<?php echo $key;?>]" name="dps_is_store_closed[<?php echo $key;?>]" <?php checked( $value, 'on' ); ?> value=""> Commented Aug 24, 2017 at 7:07
  • I think it should be for example id="<?php echo $dps_is_store_closed[$key]; ?>" - change the name like that too Commented Aug 24, 2017 at 7:10
  • The above solution resolves the current error. The next problem i am facing is on saving the post value. The checkbox that are not checked is showing Notice: Undefined offset: 3 in /path on line 63 value="on"> Commented Aug 24, 2017 at 7:27
  • For example: Array ( [0] => on [1] => on [2] => on [4] => on [5] => on [6] => on ) [3] is not inserted Commented Aug 24, 2017 at 7:28

1 Answer 1

2

Is your input tag should be:

id="dps_is_store_closed[<?php echo $key;?>]"

instead:

id="dps_is_store_closed[<?php $key?>]"

And:

name="dps_is_store_closed[<?php echo $key;?>]"

instead:

name="dps_is_store_closed[<?php $key?>]"
Sign up to request clarification or add additional context in comments.

3 Comments

The next problem i am facing is on saving the post value. The checkbox that are not checked is showing Notice: Undefined offset: 3 in /path on line 63 value="on"> For example: Array ( [0] => on [1] => on [2] => on [4] => on [5] => on [6] => on ) [3] -> '' is not inserted
Have you tried checked($dps_is_store_closed[$key], 1, false)?
After adding another value in hidden input resolved this issue For example: <input type="hidden" name="dps_is_vendor_closed[<?php echo $key?>]" value="off">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.