I have a problem, when the condition is met, $field_id_value == "1"
the checkbox is checked, but when unchecking it, php doesn’t recognise it as unchecked, it is always checked.
I have used if isset()
and empty()
but it doesn’t detect it
function wpdocs_unsub_page_callback() {
?>
<form method="post" action="<?php echo esc_html('?page=categoriasrelacionadas'); ?>">
<div>
<input type="button" onclick="checkAll()" value="CHECK ALL">
<input type="reset" value="UNCHECK ALL">
</div>
<?php
$cat_argtos = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categorias = get_categories($cat_argtos);
foreach($categorias as $categoria) {
$args = array(
'showposts' => -1,
'category__in' => array($categoria->term_id),
'caller_get_posts'=>1
);
$entradas = get_posts($args);
$field_id_value = get_option($categoria->term_id);
if($field_id_value == "1"){
$field_id_checked = 'checked="checked"';
} else {
$field_id_checked=" ";
}
echo '<p>' .$categoria->name.'<input id="myCheck'.$categoria->term_id.'" type="checkbox" '.$field_id_checked.' class="check3" name="categorias[]" value="'.$categoria->term_id.'" /> </p> ';
}
?>
</p>
<!-- #universal-message-container -->
<?php
wp_nonce_field( 'acme-settings-save', 'acme-custom-message' );
submit_button();
?>
</form>
<?php
if(isset($_POST['submit'])){//Validacion de envio de formulario
if (!isset($_POST['categorias'])) {
$checked = 0;
}
else { $checked = 1;}
foreach($_POST['categorias'] as $selected){
update_option($selected, $checked);
echo $selected."</br>";// Imprime resultados
}
}
}