Just use SELECT count(id) so that database needs to read only 1 column.
Here's my example with vanilla javascript as I hate jquery.
For a quick test, save as test.php
Here's my example with vanilla javascript as I hate jquery.
For a quick test, save as test.php
<?php
include 'backend/functions.php';
dbconn();
// This is the PHP side ajax check
if (isset($_GET['ajax']) && $_GET['ajax'] === '1')
{
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$username = sqlesc($_POST['username']);
$stmt = SQL_Query_exec("SELECT COUNT(id) as username FROM users WHERE username = $username");
$row = mysqli_fetch_assoc($stmt);
if ($row['username'] < 1)
{
$response = '<font color="green">This username is available</font>';
}
else
{
$response = '<font color="red">This username is not available</font>';
}
echo $response;
die;
}
}
stdhead('username check');
begin_frame('username check');
?>
<form method="post">
Username: <input type="text" onkeyup="usernameCheck(this.value)" name="username" /> <span id="usernameStatus"></span>
</form>
<script>
function usernameCheck(str)
{
// This is just a browser side protection (Check usernames only if username has more than 3 letters)
if (str.length < 3) {
return false;
}
// Define AJAX XMLHTTP
// Modern browsers
if (window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
// I.E 6
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = '?ajax=1';
var params = 'username=' + str;
xhttp.open("POST", url, true);
// Send the proper header information along with the request
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// What to do with the response
xhttp.onreadystatechange = function()
{
if (this.readyState === 4 && this.status === 200)
{
document.getElementById("usernameStatus").innerHTML = this.responseText;
}
};
// Send the post fields
xhttp.send(params);
// Until data returned, show custom text
document.getElementById("usernameStatus").innerHTML = "Processing...";
}
</script>
<?php
end_frame();
stdfoot();
