Himani Himani - 1 month ago 7
PHP Question

Checkbox values not saved correctly into a wordpress meta array

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>



Answer Source

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?>]"