jquery 如何根据用户选择保存复选框选择模式

w3nuxt5m  于 6个月前  发布在  jQuery
关注(0)|答案(2)|浏览(44)

我希望在提交表单时保存复选框的位置。
我的代码的问题是,如果用户启用或禁用复选框;提交表单后,复选框被标记,用户的选择不被考虑。
我的代码:

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

字符串

编辑:

我已经把这个表格上的用户帐户页面和用户选择复选框发送或不发送电子邮件的网站。
我希望当用户选择复选框模式时;保存该用户的复选框模式,以便在注销并重新输入帐户后,该用户的复选框模式仍保持不变。

oyt4ldly

oyt4ldly1#

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

字符串

h79rfbju

h79rfbju2#

你可以简单地通过检查issetempty函数来处理,当用户选中复选框时,你将获得email-post字段,否则该字段将未定义

if(!empty($_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>';
}

字符串

!empty函数检查变量是否存在和变量是否为空。希望对你有帮助。

相关问题