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 & 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);
?>