I am checking duplicate entry issue in my form with javascript, for example : When I entered duplicate username then its creating message that user already exist . (everything working fine till this ) But when I click submit, its also entered data into database by ignoring existing message, how can I stop submit button from processing if duplicate message is display before submit button. here is my code main file
<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.js"></script>
</head>
<body>
<form class="a16" action="form9.php" method="POST" autocomplete="on">
<label for="userName" class=" ">Użytkownik:</label>
<input type="text" name="userName" placeholder="Username" id="userName1" onBlur="checkAvailability()">
<span id="user-availability-status"></span>
<p><img src="LoaderIcon.gif" id="loaderIcon" style="display:none" /></p>
<input type="submit" name="submit" id="submit" value="Submit">
<script type="text/javascript">
function checkAvailability() {
$("#loaderIcon").show();
jQuery.ajax({
url: "proceser.php",
data:'userName='+$("#userName1").val(),
type: "POST",
success:function(data){
$("#user-availability-status").html(data);
$("#loaderIcon").hide();
},
error:function (){}
});
}
</script>
and here is proceser.php
<?php
include 'db1.php'; //standard datebase local connection..
if(isset($_POST['userName']) && $_POST['userName']!="") {
if ($stmt = $con->prepare('SELECT userName FROM ttt WHERE userName = ?')) {
$stmt->bind_param('s', $_POST['userName']);
$stmt->execute();
$stmt->store_result();
$numRows = $stmt->num_rows;
if ($numRows > 0) {
echo "<span class=''> Username Not Available.</span>";
} else {
echo "<span class=''> Username Available.</span>";
}
}
}
$con->close();
ob_end_flush();
?>
and in the end here is form9.php which do processing of database
<?php
// getting all values from the HTML form
if(isset($_POST['submit']))
{
$username1 = $_POST['userName'];
}
// database details
$host = "localhost";
$username = "root";
$password = "";
$dbname = "hmis";
// creating a connection
$con = mysqli_connect($host, $username, $password, $dbname);
// to ensure that the connection is made
if (!$con)
{
die("Connection failed!" . mysqli_connect_error());
}
// using sql to create a data entry query
$sql = "INSERT INTO ttt (username)
VALUES ('$username1')";
// send query to the database to add values and confirm if successful
$rs = mysqli_query($con, $sql);
if($rs)
{
echo "";
}