Issue
I want the position of the checkbox to be saved when my form is submitted.
The problem with my code is that if the user enables or disables the checkbox; after submitting the form, the checkbox is marked and the user's choice is not taken into account.
My code:
<?php
function tets_checkBox() {
if (isset($_POST['submit-post'])) {
if (isset($_POST['email-post'])) {
echo '<p class="alert alert-success">' . __('The checkbox is checked.') . '</p>';
} else {
echo '<p class="alert alert-danger">' . __('The checkbox is not checked.') . '</p>';
}
echo '<p class="alert alert-success">' . __('The settings for your account have been updated') . '</p>';
}
}
?>
<form method="post" action=" ">
<aside class="account_news">
<label class="account_mainBtn">
<input type="checkbox" name="email-post" checked>
</label>
<h3>Select.</h3>
</aside>
<input type="submit" name="submit-post" value="<?php _e('Update settings'); ?>">
</form>
Edit:
I have placed this form on the user account page and the user selects the check box to send or not send email by the site.
I want when the user selects the checkbox mode; Save the checkbox mode for that user so that after logging out and re-entering the account, the checkbox mode is still maintained for that user.
Solution
$checked_attr = "checked='checked'";
$flag = 0;
if ( isset( $_POST['submit-post'] ) ) {
$flag = 1;
}
if ( 1 === $flag ) {
$checked_attr = '';
if ( isset( $_POST['email-post'] ) && ! empty( $_POST['email-post'] ) ) {
$checked_attr = "checked='checked'";
}
}
tets_checkBox();
?>
<form method="POST" action="" >
<aside class="account_news">
<label class="account_mainBtn">
<input type="checkbox" name="email-post" <?php echo $checked_attr; ?> >
</label>
<h3>Select.</h3>
</aside>
<input type="submit" name="submit-post" value="<?php echo( 'Update settings' ); ?>">
</form>
Answered By - Neha Bahadure
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.