duanjuda5789 2018-12-10 19:07
浏览 82
已采纳

使用bootstrap验证程序检查重复的mysql数据

I created a signup form with the bootstrap validator and connected it to a MySQL database. I wish to prevent duplicate entries by checking for existing email data and returning a message through the bootstrap validator script. I am using the remote method but the problem is as soon as I start typing in the email field, I get the "Email has already been taken." message although the email may not already exist in the database or if it is only one alphabet typed. I also get data entries even when I have not submitted the form. I have searched online and realized most of the solutions exist for the jquery validation plugin but no the bootstrap validator. I need help, please. Here is my code:

signup.php

 <div class="signup-form">
 <h1>Sign up for free!</h1><br>

 <form id="form1" action="registration.php" class="loading-form" method="POST">

<div class="form-group">
  <label for="name-input">Username</label>
  <input required type="text" name="username" class="form-control" id="username" maxlength="100">
</div>    

  <label for="email-input">Email</label>
  <input required name="email" type="email" class="form-control" id="email" title="An email is required">
</div>

<p>You accept our <a href="pages/terms.php" style="color: #337ab7;">Terms &amp; Conditions</a> by creating your account.</p>

<div class="form-group">
<!-- Do NOT use name="submit" or id="submit" for the Submit button -->
<button type="submit" class="btn btn-success">Sign up</button>
</div>
  <script>
  $(document).ready(function() {
  $('#form1').bootstrapValidator({
    // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        username: {
            message: 'The username is not valid',
            validators: {
                notEmpty: {
                    message: 'The username is required'
                },
                stringLength: {
                    min: 6,
                    max: 30,
                    message: 'The username must be more than 6 and less than 30 
        characters long'
                },
                regexp: {
                    regexp: /^[a-zA-Z-' ]+$/,
                    message: 'The username can only consist of alphabetical and number'
                },
                different: {
                    field: 'password',
                    message: 'The username and password cannot be the same as each other'
                }
            }
        },
        email: {
            validators: {
                notEmpty: {
                    message: 'The email address is required'
                },
                emailAddress: {
                    message: 'The email address is not a valid'
                },
                remote: {
                    message: 'The email address is already taken.',
                    url: "registration.php"
                }
            }
        },
    }
});
});

registration.php

<?php   
include ("connect.php");
session_start();

$username = $_POST['username'];
$email = $_POST['email'];

$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);

$query1 = "SELECT * FROM registration where (email='$email')";
$check = mysqli_query($con, $query1);
$checkrows=mysqli_num_rows($check);

if($checkrows>0) {
echo json_encode(FALSE);
}
else
{
echo json_encode(TRUE);
}

//insert results from the form input
$query = "INSERT INTO registration (username, email) VALUES('$username', '$email')";
$result = mysqli_query($con, $query);
$num1=mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);

?>
  • 写回答

3条回答 默认 最新

  • dongqiao1888 2019-01-09 22:36
    关注

    Here is the the code that worked for me:

    registration

    header('Content-type: application/json');
    
    $valid = true;
    $message = '';
    
    ob_start();
    error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
    
    include ("connect.php");
    session_start();
    
    //get the name and comment entered by user
    $fullName = $_POST['fullName'];
    $userName = $_POST['userName'];
    $birthday = $_POST['birthday'];
    $gender = $_POST['gender'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    
    
    $fullName = mysqli_real_escape_string($con, $_POST['fullName']);
    $userName = mysqli_real_escape_string($con, $_POST['userName']);
    $birthday = mysqli_real_escape_string($con, $_POST['birthday']);
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $password = mysqli_real_escape_string($con, $_POST['password']);
    
    //insert results from the form input
    $query1 = "SELECT * FROM registration where (email='$email')";
    $check = mysqli_query($con, $query1);
    $checkrows=mysqli_num_rows($check);
    
    if($checkrows > 0) {
       // If the email address exists in the table, the ``$valid`` variable define as false.
       $valid = false;
       $message = 'The email address exists.';
    }
    else
    {
    
    }
    
    echo json_encode(
    $valid ? array('valid' => $valid) : array('valid' => $valid, 'message' =>     $message)
    );
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波