admin管理员组文章数量:1130349
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
foreachlike so:foreach ( $orderdescider as $i => $item ).Then inside that
foreach,$idand$checkboxshould 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
checkboxfields, 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
1is the{value}, and$checkbox_checkis 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
}
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
foreachlike so:foreach ( $orderdescider as $i => $item ).Then inside that
foreach,$idand$checkboxshould 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
checkboxfields, 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
1is the{value}, and$checkbox_checkis 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
}
本文标签: optionsUsing getoption() for check box field
版权声明:本文标题:options - Using get_option() for check box field 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749186460a2329468.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论