I am trying to have a check field saved to database. it works . but in get_option i am passing the default value. but even if disable the checkbox(value 0) the field shows as ticked ..this is my code
<?php
$fields_default = array(
0 => array(
'id' => 0,
'checkbox_check' => '1',
),
1 => array(
'id' => 1,
'checkbox_check' => '0',
));
$orderdescider = get_option('fields_new', $fields_default);
foreach ($orderdescider as $checkfieldstatus) {
if (isset($checkfieldstatus['id'])) {$id = $checkfieldstatus['id'];}
if (isset($checkfieldstatus['checkbox_check'])) {$checkbox_check = $checkfieldstatus['checkbox_check'];} else { $checkbox_check = '0';}
?>
<li class="sortable-item flexit">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1" name="orderdescider[<?php echo $id; ?>][checkbox_check]" value="1"<?php checked(isset($orderdescider[$id]['checkbox_check']));?> />
</div></li>
<?php
}
please help thanks
I am trying to have a check field saved to database. it works . but in get_option i am passing the default value. but even if disable the checkbox(value 0) the field shows as ticked ..this is my code
<?php
$fields_default = array(
0 => array(
'id' => 0,
'checkbox_check' => '1',
),
1 => array(
'id' => 1,
'checkbox_check' => '0',
));
$orderdescider = get_option('fields_new', $fields_default);
foreach ($orderdescider as $checkfieldstatus) {
if (isset($checkfieldstatus['id'])) {$id = $checkfieldstatus['id'];}
if (isset($checkfieldstatus['checkbox_check'])) {$checkbox_check = $checkfieldstatus['checkbox_check'];} else { $checkbox_check = '0';}
?>
<li class="sortable-item flexit">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1" name="orderdescider[<?php echo $id; ?>][checkbox_check]" value="1"<?php checked(isset($orderdescider[$id]['checkbox_check']));?> />
</div></li>
<?php
}
please help thanks
Share Improve this question edited Nov 7, 2018 at 16:51 asked Nov 7, 2018 at 16:13 user145078user145078 20- 1 I'm unable to replicate your code, I get the following error code: Parse error: syntax error, unexpected '$orderdescider' (T_VARIABLE) – Remzi Cavdar Commented Nov 7, 2018 at 16:23
- Could you paste all your code, so that I can take a look at it? – Remzi Cavdar Commented Nov 7, 2018 at 16:23
- @RemziCavdar actually the array was not closed, please have a check now – user145078 Commented Nov 7, 2018 at 16:25
- 1 I also get Notice: Undefined variable: value_new – Remzi Cavdar Commented Nov 7, 2018 at 16:49
- 1 I don't know the answer, but I get help you out with your coding style, so that it is more readble, see: pastebin/Eimh4RZ5 – Remzi Cavdar Commented Nov 7, 2018 at 17:00
1 Answer
Reset to default 0This may not exactly answer the question, but let's see the issues with your code:
If you want to save the data in the same format as the default value (
$fields_default
), then:Start your
foreach
like so:foreach ( $orderdescider as $i => $item )
.Then inside that
foreach
,$id
and$checkbox
should always be set and defined like so:$id = isset( $item['id'] ) ? $item['id'] : ''; $checkbox_check = isset( $item['checkbox_check'] ) ? $item['checkbox_check'] : '';
And use the appropriate value in the field's
name
:<input type="hidden" name="orderdescider[<?php echo $i; ?>][id]" value="<?php echo $id; ?>"> <input type="checkbox" name="orderdescider[<?php echo $i; ?>][checkbox_check]" value="1" ...>
For the
checkbox
fields, you can use this format when writing the markup:<input type="checkbox" value="{value}"<?php // wrapped for clarity checked( '{value}', '{current/saved value}' ) ?> ...>
so in your case, it would look like this:
<input type="checkbox" name="orderdescider[<?php echo $i; ?>][checkbox_check]" value="1"<?php checked( '1', $checkbox_check ) ?> ...>
where
1
is the{value}
, and$checkbox_check
is the{current/saved value}
.(See the reference for more details about using the
checked()
function.)
Hope that helps, and here's the full foreach
code I used for testing: (I omitted the field's id
, but make sure to use unique values)
foreach ($orderdescider as $i => $item) {
$id = isset( $item['id'] ) ? $item['id'] : '';
$checkbox_check = isset( $item['checkbox_check'] ) ? $item['checkbox_check'] : '';
?>
<li class="sortable-item flexit">
<div class="form-check">
<input type="hidden" name="orderdescider[<?php echo $i; ?>][id]"
value="<?php echo $id; ?>">
<input type="checkbox" name="orderdescider[<?php echo $i; ?>][checkbox_check]"
value="1"<?php checked( '1', $checkbox_check ) ?> class="form-check-input">
</div>
</li>
<?php
}