$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - How to get correct value from checked()?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - How to get correct value from checked()?

matteradmin9PV0评论

I am trying to create 2 raido buttons as a category custom field, and in function.php I do:

$feat = get_term_meta( $tag->term_id, '_feat', true ); 

<input name="feat" type="radio" value="0" <?php checked( '0' ); ?> />Si<br>
<input name="feat" type="radio" value="1" <?php checked( '1' ); ?> />No

And then I do

if ( isset( $_POST['feat'] ) )
  update_term_meta( $_POST['tag_ID'], '_feat', $_POST['feat'] );

But I always get "No" as checked

I am trying to create 2 raido buttons as a category custom field, and in function.php I do:

$feat = get_term_meta( $tag->term_id, '_feat', true ); 

<input name="feat" type="radio" value="0" <?php checked( '0' ); ?> />Si<br>
<input name="feat" type="radio" value="1" <?php checked( '1' ); ?> />No

And then I do

if ( isset( $_POST['feat'] ) )
  update_term_meta( $_POST['tag_ID'], '_feat', $_POST['feat'] );

But I always get "No" as checked

Share Improve this question asked Nov 23, 2018 at 12:12 rob.mrob.m 2072 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The purpose of checked() is to output a checked="checked" attribute based on a current value. By using it the way you're using it there you're forcing Si to never be checked and No to always be checked.

So what you want to do is use both arguments of checked() to compare the value of the input to the current value:

<input name="feat" type="radio" value="0" <?php checked( $feat, '0' ); ?> />Si<br>
<input name="feat" type="radio" value="1" <?php checked( $feat, '1' ); ?> />No

With that change if $feat is '0' then the first checked will run, and if it's '1' the second will run.

Post a comment

comment list (0)

  1. No comments so far