简体   繁体   中英

How to check if username exists on the fly in a form?

I have a page with a register form.

"Username" is one of the fields filled out by users.

The form submits to another php page, which checks if the username is already taken or not. If taken, then I need to go history.back() and at the same time set a session variable, so the previous page knows that the username exists, and display an error message.

Is this possible? Ie is php code executed when going back in browsers?

It would be alot of extra work if I had to use sessions and do a simple redirect, because then I would have to "Remember" all the other form-inputs as well, so the user doesn't have to fill the entire form out again.

OR

maybe there is another way of checking whether a username is busy or not.

I use MySql as DB, so thats where the usernames are stored.

Thanks

how about using AJAX?.. you wont need to reload nor change the page.

I have seen many sites that have a link next to the username input to validate the user name.. or you could do it automatically on keypress... good luck

While you are correct in saying you'd have to 'remember' the form inputs, why do you not do the checks and output a block of JS that will redirect to the login page using the values posted if the username already exists. Then, have the login page pre-populate the login input with these values.

Using this method, you will be able to set your session variable, redirect the user back to the login page (with an error message if you wish) AND repopulate the inputs with the previously posted variables.

Using the back() function is definitely not the way to go about this. Avoid back() for things like this at all costs.

To answer your question regarding PHP being executed when you go back() - this probably depends on the caching policy of the page and the behavior of the browser. As such, this would not be a reliable way of sending data back to the form.

An alternative would be to use AJAX to check the username. That way you would not have to leave the registration page at all, as availability would be checked BEFORE submitting the registration form.

A good idea would be to process the form in the same script, above where your form is generated. That way, you can user $_POST global array. For example:

<?php
if (isset($_POST['submit'])) {
    // do chat name check here
    $chat_name = mysql_real_escape_string($_POST['chat_name']);
    $sql = "SELECT COUNT(*) AS count FROM users WHERE chat_name = '$chat_name'";
    $res = mysql_query($sql);
    $row = mysql_fetch_assoc($res);
    if ($row['count'] > 0) {
        $chat_name_exists = true;
    } else {
        $chat_name_exists = false;
    }
}

if ($chat_name_exists) {
    echo '<p class="error">Sorry, but that chat name already exists</p>';
}

$chat_name = (isset($_POST['chat_name'])) ? htmlentities($_POST['chat_name']) : "";

echo '<form action="" method="post">';
echo '<input type="text" name="chat_name" value="' . $chat_name . '" />';
echo '<input type="submit" name="submit" value="Check" />';
echo '</form>';

This was written off the cuff, so don't copy-and-paste but integrate into your application as it best fits. You also eliminate the need to rely on JavaScript this way, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM