There is a task - this is registration of the user and saving it in the phpmyadmin database. When sending a GET or POST request to a form, you need to press send. After that, the page is updated. Only after that you can send the user information about what we assume such a nickname or email is already in the database. This approach does not suit. We need another implementation where the user just entered his nickname and email in the field
(he hasn’t pressed to send yet) and he will immediately see that such a nickname is already taken. This is the type of implementation of registration on mail.ru. Is it possible to implement this in php in principle? Thank you in advance.
- The idea is not bad, but it’s obsolete for 10 years))) Now it only looks like a handy tool for parsing your database))) Let the data not be very secret, but I wouldn’t want someone to know ALL users' nicknames with my store ... Especially since half comes up with a nickname one-on-one with an email))) Well, as for updating the page - well, there is ajax !! - Sergey V.
- we are talking only about the test version, we are not talking about the database in general in this matter. - alex
|
1 answer
Using JS is possible.
For example:
Customer:
myPerfectEmailInput.addEventListener("change", e => { if (!e.target.value.match(/@/) || e.target.value.length < 4) return false; // дабы лишний раз сервер не дёргать. let formData = new FormData(); formData.append("email", e.target.value); // вдруг ещё что-то передать нужно. fetch("/url/to/handler.php", { method: "POST", body: formData }) .then(r => r.json()) // подразумевается, что ответ с сервера - JSON .then(r => { if (r.emailExists) { doSomething(); // например, выдаём какую-нибудь ошибку/подсвечиваем инпут красным или что-то в этом духе. } }); }); <input type="email" id="myPerfectEmailInput" /> PHP:$result = $db->query("SELECT 1 AS exists FROM users WHERE email = ?", $_POST['email']); // здесь какой-то запрос в БД. echo json_encode(["emailExists" => $result["exists"]);
It can work like this . (if anything, busy or not busy - random, just for example)
UPD. Also added a check on the number of characters, he did not think about it. Thanks to Sergey .
- Add - it is desirable to limit the requests, i.e. do not send a request to the server for each letter, but wait until you type 3-4-5 characters. Just imagine that bots came to your site on a massive scale and started writing text on the form, the server will say thank you or not? Anyway, there is no practical benefit from this, who wants to register, wants to, and who fool around, will be cut off by the limit. - Sergey V.
- one@ SergeyV., Rules the answer, thank you. - ya.ymer
|