6
Hey there, you guys! Currently, to check if a username is unique, I do it in the most basic way. Sending the POST to a PHP page that checks in Mysql, only then returns the error. php.:
<form role="form" action="https://<?php print $_SERVER['HTTP_HOST']; ?>/cadastro/" enctype="multipart/form-data" method="post">
<div class="form-group">
<label>Usuário</label>
<input name="usuario" type="text" class="form-control" minlength="4" placeholder="Digite um usuário único..." required>
</div>
<button type="submit" class="btn">Criar</button>
</form>
php checks.:
<?php
$SqlCheck = $ConDB -> prepare("SELECT COUNT(*) AS `ContUser` FROM
`usuarios` WHERE `userName`=:USER;");
$SqlCheck -> bindParam(":USER", $_POST['usuario']);
$SqlCheck -> execute();
$RowCheck = $SqlCheck -> fetch(PDO::FETCH_OBJ);
$ContUser = $RowCheck -> ContUser;
if ($ContUser == 0) {
/* Realiza o cadastro */
} else {
echo 'Usuário já existe';
}
?>
Is it possible to do this check while typing in the input, for example, from the 4 character is shown in the bottom row if the user already exists? In real time?
Maybe in jQuery or ajax you can do, but I don’t know any of these languages.
The bad news is that after the 4th character you will stress your server with successive requests.
– Sam
I find this interesting, but I’m not really into it because I never needed it or didn’t find it necessary for my projects. But to avoid the many requests to the bank, which would be bad, perhaps an idea would be to create a text file with all the names of users already registered and, instead of consulting the bank, consult this file. And go updating this file from time to time.
– Sam
Have how many names in the table?
– user60252
Today there is only one. The system is not online yet. I want to learn to do the same in Gmail registration. When he takes the input focus, he says if there is already a user, in the div below: https://uploaddeimagens.com.br/imagens/crie_sua_conta_do_google_-_mozilla_firefox_2017-11-01_21-48-14-png
– Márcio Teixeira
From the fourth character then I would not exist hahaha
– user60252
You can do it in a good way (and without stressing the server) if you make a timer. For example, when the field changes, it waits a little while and does the checking, so it doesn’t just trigger requests while typing. That’s how many systems in production do today. Often the answer lies on the very sites you’ve visited with the feature. Or just do it in the same focus loss, there is much less requisition.
– Bacco