我使用引导验证器创建了一个注册表单,并将其连接到MySQL数据库。我希望通过检查现有的电子邮件数据并通过引导程序验证程序脚本返回消息来防止重复条目。我正在使用远程方法,但是问题是,一旦我开始在电子邮件字段中输入内容,就会收到“电子邮件已被接收”的信息。消息,尽管该电子邮件可能尚未存在于数据库中,或者仅输入了一个字母。即使没有提交表单,我也可以获取数据条目。我已经在线搜索并意识到,大多数解决方案都适用于jquery验证插件,但没有引导程序验证器。我需要帮助。这是我的代码:
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 & 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);
?>
答案 0 :(得分:0)
在“ registration.php”文件的末尾,您将在数据库中插入接收到的信息。
这意味着您要插入重复项,甚至插入格式错误的电子邮件地址。另外,如果您的JavaScript试图在每次按键时进行验证,则您要插入每个字母。例如,如果您要注册test@example.com,则您的注册使用电子邮件“ t”,另一个使用“ te”,依此类推,直到完整的电子邮件为止。
可能是问题所在。
您需要进行一些修改: 首先:对接收到的值进行一些检查。例如,如果checkrows> 0,则永远不要执行插入查询。同时检查电子邮件格式是否正确,在PHP方面。
此外,仅当用户单击“提交”时,才尝试写入表。
您可以使用两个文件:一个用于检查,另一个用于验证并将结果写入表。
答案 1 :(得分:0)
首先,当表中存在电子邮件地址时,您的php文件永远不会停止脚本。因为您的if语句有问题。
<?php
header('Content-type: application/json');
$valid = true;
$message = '';
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) {
// If the email address exists in the table, the ``$valid`` variable define as false.
$valid = false;
$message = 'The email address exists.';
}
else
{
// If the email address has not exists in the table, adds new record to database and $valid variable returns as 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);
}
echo json_encode(
$valid ? array('valid' => $valid) : array('valid' => $valid, 'message' => $message)
);
?>
请从官方仓库中查看remote.php演示文件。 https://github.com/nghuuphuoc/bootstrapvalidator/blob/master/demo/remote.php
答案 2 :(得分:0)
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)
);
?>