我希望在提交表单时保存复选框的位置.

我的代码的问题是,如果用户启用或禁用复选框;在提交表单后,复选框被标记,并且不考虑用户的 Select .

我的代码是:

<?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:

我已将此表单放在用户帐户页面上,用户选中复选框以通过站点发送或不发送邮箱.

我希望当用户 Select 复选框模式时;保存该用户的复选框模式,以便在注销并重新输入帐户后,仍保持该用户的复选框模式.

推荐答案

$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>

Php相关问答推荐

无法添加ProxyClass__setInitialized()上的忽略

为什么注册SIGHUP的处理程序函数会阻止在PHP CLI中等待输入时点击X关闭Xterm窗口?""

PHP邮件表单无法识别重音/特殊字符

从WooCommerce Checkout Country Select2下拉列表中重新排序国家/地区

PHP-带POST验证的for循环

WordPress:今天的计数';修改和发布的帖子

从WooCommerce订单项目获取Dokan卖家详细信息

更改WooCommerce checkout 中的错误消息以获得不可用的送货方式

允许在WooCommerce管理订单列表中使用计费邮箱进行搜索

execute_query 和prepare+execute 有什么区别?

invalid_grant 和无效的 JWT 签名

为WooCommerce中的Guest用户保存唯一的客户代码到订单中

.htaccess环境变量条件不被判断

仅允许减少 WooCommerce 中已完成订单的库存数量

如何获取用户之间的最新消息列表

带有 nginx 的 Symfony 不提供 .js 和 .css 文件

如何在供应商名称后将自定义徽章添加到商品详情

在 laravel 中更新表单时如何在 Select 选项中保留旧值?

标头内容类型:image/jpeg 无效

使用 PHP 有条件地使用关键字 Twilio Programmable SMS