• Resolved Guido

    (@guido07111975)


    Hi,

    Rookie question, but I’m having trouble echoing a checkbox field in php syntax:

    <input name="my_field" type="checkbox" id="my_field" value="yes" <?php checked('yes', $value ); ?> />

    Obviously this doesn’t work:

    echo ' <input name="my_field" type="checkbox" id="my_field" value="yes" <?php checked('yes', $value ); ?> /> ';

    Who can help me out.. or maybe I just need some sleep.. 😉

    Guido

Viewing 4 replies - 1 through 4 (of 4 total)
  • Normalyl I’d do it like this…

    <input name="my_field" type="checkbox" id="my_field" value="yes"<?php if ($value == 'yes') echo ' checked'; ?> />

    You need to escape the quotes within the echo and your syntax is not valid

    <?php echo '<input name="my_field" type="checkbox" id="my_field" value="yes" checked(\'yes\', '. $value .' ) /> '; ?>

    also make your output safe by using htmlspecialchars. i.e

    <?php echo '<input name="my_field"
    type="checkbox" id="my_field" value="yes" checked(\'yes\', '.htmlspecialchars($value) .' ) /> '
    ; ?>
    Thread Starter Guido

    (@guido07111975)

    Thanks, will work with this.

    @wooninjas: I don’t need to escape it there too because I do it 1 step earlier:

    $value = esc_attr( get_option( 'my_field' ) );

    Guido

    Ok. @guido in that case you don’t need htmlspecialchars

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Echo checkbox input field’ is closed to new replies.