2017-05-06 58 views
0

的方面,我想知道我如何設置一個複選框,上面寫着......複選框確認服務

同意的條款和條件

但用戶只能點擊註冊按鈕,如果複選框已被點擊。

這裏是我的整個註冊頁面代碼的大部分。

<body class="login-body"> 
 
\t \t 
 
\t \t <div class="container"> 
 
\t \t \t <form class="form-signin" action="/register" method="POST"> 
 
\t \t \t \t <h2 class="form-signin-heading"><?php echo $website;?></h2> 
 
\t \t \t \t <div class="login-wrap"> 
 
\t \t \t \t \t <?php 
 
\t \t \t \t \t \t if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['confirmpassword']) && isset($_POST['email'])) { 
 
\t \t \t \t \t \t \t $username = mysqli_real_escape_string($con, $_POST['username']); 
 
\t \t \t \t \t \t \t $password = mysqli_real_escape_string($con, md5($_POST['password'])); 
 
\t \t \t \t \t \t \t $confirmpassword = mysqli_real_escape_string($con, md5($_POST['confirmpassword'])); 
 
\t \t \t \t \t \t \t $email = mysqli_real_escape_string($con, $_POST['email']); 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t if($password != $confirmpassword) { 
 
\t \t \t \t \t \t \t \t echo "<div class=\"alert alert-danger\">The passwords you entered do not match.</div>"; 
 
\t \t \t \t \t \t \t \t $error = 'yes'; 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t if(strlen($_POST['password']) < 8) { 
 
\t \t \t \t \t \t \t \t echo "<div class=\"alert alert-danger\">Your password must be atleast 8 characters!</div>"; 
 
\t \t \t \t \t \t \t \t $error = 'yes'; 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
 
\t \t \t \t \t \t \t \t $error = 'yes'; 
 
\t \t \t \t \t \t \t \t echo "<div class=\"alert alert-danger\">The email you entered is invalid.</div>"; 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t $result = mysqli_query($con, "SELECT * FROM `users` WHERE `username` = '$username'") or die(mysqli_error($con)); 
 
\t \t \t \t \t \t \t if (mysqli_num_rows($result) > 0) { 
 
\t \t \t \t \t \t \t \t $error = 'yes'; 
 
\t \t \t \t \t \t \t \t echo "<div class=\"alert alert-danger\">This username already exists.</div>"; 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t $result = mysqli_query($con, "SELECT * FROM `users` WHERE `email` = '$email'") or die(mysqli_error($con)); 
 
\t \t \t \t \t \t \t if (mysqli_num_rows($result) > 0) { 
 
\t \t \t \t \t \t \t \t $error = 'yes'; 
 
\t \t \t \t \t \t \t \t echo "<div class=\"alert alert-danger\">The email you entered can not be used.</div>"; 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t $ip = mysqli_real_escape_string($con, $_SERVER['REMOTE_ADDR']); 
 
\t \t \t \t \t \t \t $date = date('Y-m-d'); 
 
\t \t \t \t \t \t \t if ($error != 'yes') { 
 
\t \t \t \t \t \t \t \t mysqli_query($con, "INSERT INTO `users` (`username`, `password`, `email`, `date`, `ip`) VALUES ('$username', '$password', '$email', '$date', '$ip')") or die(mysqli_error($con)); 
 
\t \t \t \t \t \t \t \t header("Location: /login?action=registered"); 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t ?> 
 
\t \t \t \t \t <input type="text" id="username" name="username" class="form-control" placeholder="Username" value="<?php echo $_POST['username'] ?>" autofocus> 
 
\t \t \t \t \t <input type="password" id="password" name="password" class="form-control" placeholder="Password"> 
 
\t \t \t \t \t <input type="password" id="confirmpassword" name="confirmpassword" class="form-control" placeholder="Confirm Password"> 
 
\t \t \t \t \t <input type="text" id="email" name="email" class="form-control" placeholder="Email" value="<?php echo $_POST['email'] ?>"> 
 
\t \t \t \t \t <button class="btn btn-lg btn-login btn-block" type="submit">Register</button> 
 
\t \t \t \t </form> 
 
\t \t \t \t 
 
\t \t \t \t <div class="registration"> 
 
\t \t \t \t \t Already have an account?&nbsp 
 
\t \t \t \t \t <a class="" href="/login"> 
 
\t \t \t \t \t \t Sign In 
 
\t \t \t \t \t </a> 
 
\t \t \t \t </div> 
 
\t \t \t \t 
 
\t \t \t </div> 
 
\t \t \t 
 
\t \t \t 
 
\t \t </div><!-- end .container -->

+0

的可能的複製[簡單的JavaScript複選框驗證](http://stackoverflow.com/questions/11234622/simple-javascript-checkbox-validation) – Picard

回答

0

設置在頁面加載寄存器button disabled和基於複選框的變化而變化的disabled true or false

$('#submit_form').prop('disabled',true); 
 

 
$('#terms').change(function() 
 
{ 
 
    $('#submit_form').prop('disabled',!$(this).is(':checked')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" name="terms" id="terms" /> 
 

 
<input type="submit" id="submit_form" value="register" />

0

您可以使用內嵌的JavaScript,並使用onchange事件

<input type="checkbox" onchange="document.getElementById('submit_form').disabled = !this.checked;" /> 

<input type="submit" id="submit_form" value="register" />