javascript 如何在提交之前根据 SQL 数据库检查输入值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6420802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check input values against SQL database BEFORE submission?
提问by Alex Eyler
I want to be able to check if a user-name is not already taken before it is submitted. I've thought about using Javascript to get the value of the input field but how would I compare that to my SQL database?
我希望能够在提交之前检查用户名是否尚未被采用。我想过使用 Javascript 来获取输入字段的值,但我如何将它与我的 SQL 数据库进行比较?
Basically, the question boils down to a couple of different questions:
基本上,这个问题归结为几个不同的问题:
Can I somehow get the values of an input-field through PHP BEFORE submission?
IF NOT, then can I somehow use a Javascript variable in PHP?
IF NOT, is there something else I can do?
我可以在提交之前通过 PHP 以某种方式获取输入字段的值吗?
如果不是,那么我可以以某种方式在 PHP 中使用 Javascript 变量吗?
如果没有,我还能做些什么吗?
If I can't do this, it's no big deal, I just always see sites that check to see if a field is valid run-time instead of having to reload the page or going to an error page, which is something that I definitely want to do as well.
如果我不能这样做,也没什么大不了的,我总是看到一些网站会检查字段是否在运行时有效,而不必重新加载页面或转到错误页面,这是我肯定的也想做。
NOTE: I already know how to check to see if a user-name is taken, I just want to know if I can do it run-time, so there's no need to supply me with the SQL code haha.
注意:我已经知道如何检查用户名是否被占用,我只想知道我是否可以在运行时进行,所以不需要向我提供 SQL 代码哈哈。
Thanks!
谢谢!
回答by key2
Anon is right. You have to use ajax for this purpose. You can't simply use your javascript variable in php. Use ajax in your javascript validation and throw warning message if username already exist in your database.
安安是对的。为此,您必须使用 ajax。您不能简单地在 php 中使用您的 javascript 变量。如果用户名已存在于您的数据库中,请在您的 javascript 验证中使用 ajax 并抛出警告消息。
For reference check out this link http://www.9lessons.info/2008/12/twitter-used-jquery-plug-in.htmland http://web.enavu.com/tutorials/checking-username-availability-with-ajax-using-jquery/
作为参考,请查看此链接 http://www.9lessons.info/2008/12/twitter-used-jquery-plug-in.html和 http://web.enavu.com/tutorials/checking-username-availability- with-ajax-using-jquery/
回答by DKSan
You can achieve that by using ajax. The following example makes use of JQueryand its ajax function. The serverside script has to be written by you and should check the provided username against your database.
您可以通过使用 ajax 来实现这一点。以下示例使用了JQuery及其 ajax 函数。服务器端脚本必须由您编写,并且应该根据您的数据库检查提供的用户名。
$.ajax({
type: "POST",
url: "your_ajax_function.php",
data: "username=" + $('#id_of_username_input'),
success: function(retval){
if (retval == true)
// Username not in use, tell your visitor
else {
// Username in use, tell your visitor to change
}
}
}};
The returnvalue depends on your serverside-script and may be just the other way round. You can return whatever you want.
返回值取决于您的服务器端脚本,并且可能正好相反。你可以返回任何你想要的。