我用引导验证程序创建了一个注册表单,并将其连接到一个mysql数据库。我希望通过检查现有电子邮件数据并通过引导验证程序脚本返回消息来防止重复条目。我正在使用远程方法,但问题是,一旦我开始在电子邮件字段中键入,我就会收到“电子邮件已被接收”消息,尽管该电子邮件可能尚未存在于数据库中,或者仅键入了一个字母。即使我还没有提交表单,我也会得到数据条目。我在网上搜索了一下,发现大多数解决方案都是针对jquery验证插件的,但没有针对引导验证程序的。我需要帮助。这是我的密码:
注册.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 & 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"
}
}
},
}
});
});
注册.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条答案
按热度按时间iq0todco1#
首先,当表中存在电子邮件地址时,php文件从不停止脚本。因为你的if语句有问题。
请查看官方repo的remote.php演示文件。https://github.com/nghuuphuoc/bootstrapvalidator/blob/master/demo/remote.php
7cjasjjr2#
在“registration.php”文件的末尾,您将在数据库中插入接收到的信息。
这意味着你插入了重复的,甚至是格式错误的电子邮件地址。另外,如果您的javascript尝试在每次按键时进行验证,则插入每个字母。例如,如果您正在注册test@example.com,你有一个用电子邮件“t”注册的,另一个用“te”注册的,依此类推直到完成电子邮件。
也许,这就是问题所在。
您需要做一些修改:首先:对接收到的值执行一些检查。例如,如果checkrows>0,则从不执行insert查询。还要检查电子邮件的格式是否正确,在php端。
此外,请尝试仅在用户单击submit时写入表。
您可以使用两个文件:一个用于检查,另一个用于验证并将结果写入表。
fcg9iug33#
以下是对我有效的代码:
注册