I've got a form with several checkboxes. You can add new form OR edit old one. This is where things get tricky.
If I submit form, PHP only gets values from CHECKED checkboxes. Now I update the value in database.
If I later update/edit the form and want to set checkbox UNCHECKED, it doesn't get the value and it doesn't update the value in database.
How to solve this issue? Please note that hidden input with same name is not an option because I've got several checkboxes with same names as like this: name="name[]"
.
<?php
//Get value from dp - for editing
$feature_one = get_term_meta( $term->term_id, 'feature_one', true );
$feature_two = get_term_meta( $term->term_id, 'feature_two', true );
$feature_three = get_term_meta( $term->term_id, 'feature_three', true );
//Etc, 25 in total
?>
<input type="checkbox" name="feature[]" id="feature-one" value="feature_one" <?php if (in_array("3", $feature_one)) { echo 'checked'; } ?> />
<input type="checkbox" name="feature[]" id="feature-two" value="feature_two" <?php if (in_array("3", $feature_two)) { echo 'checked'; } ?> />
<input type="checkbox" name="feature[]" id="feature-three" value="feature_three" <?php if (in_array("3", $feature_three)) { echo 'checked'; } ?> />
<!-- 25 in total -->
<?php
//Add (or update if editing) values on submit
foreach( $_POST['feature'] as $feature ) {
if( isset( $feature) && '' !== $feature ) {
$n_feature = $feature;
update_term_meta( $term_id, $n_feature , 1 );
}
} ?>
Aucun commentaire:
Enregistrer un commentaire