简体   繁体   English

如何检查用户名是否以表格形式即时存在?

[英]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. 表单提交到另一个php页面,该页面检查用户名是否已被使用。 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. 如果采用,那么我需要进入history.back()并同时设置一个会话变量,因此上一页知道用户名存在,并显示错误消息。

Is this possible? 这可能吗? Ie is php code executed when going back in browsers? 即在浏览器中返回时是否执行了php代码?

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. 我使用MySql作为数据库,所以多数民众赞成在存储用户名的位置。

Thanks 谢谢

how about using AJAX?.. you wont need to reload nor change the page. 如何使用AJAX?..您将不需要重新加载或更改页面。

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. 正确地说,您必须“记住”表单输入,但为什么不进行检查并输出一个JS块,它将使用发布的值(如果用户名已经存在)重定向到登录页面。 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. 使用back()函数绝对不是解决此问题的方法。 Avoid back() for things like this at all costs. 不惜一切代价避免back()这样的事情。

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. 要回答有关在back()时执行PHP的问题-这可能取决于页面的缓存策略和浏览器的行为。 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. 一种替代方法是使用AJAX来检查用户名。 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. 这样,您可以使用$_POST全局数组。 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. 您还消除了以这种方式依赖JavaScript的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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