如何在提交之前根据 SQL 数据库检查输入值?
我希望能够检查用户名在提交之前是否尚未被占用。我考虑过使用 Javascript 来获取输入字段的值,但是我如何将其与我的 SQL 数据库进行比较?
基本上,这个问题可以归结为几个不同的问题:
我可以在提交之前通过 PHP 以某种方式获取输入字段的值吗?
如果不是,那么我可以在 PHP 中使用 Javascript 变量吗?
如果没有,我还能做些什么吗?
如果我做不到这一点,也没什么大不了的,我只是总是看到一些网站检查某个字段是否在运行时有效,而不必重新加载页面或转到错误页面,这是我绝对要做的事情也想做。
注意:我已经知道如何检查用户名是否被占用,我只是想知道我是否可以在运行时执行此操作,因此无需向我提供 SQL 代码哈哈。
谢谢!
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?
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?
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阿诺是对的。
为此,您必须使用 ajax。你不能简单地在 php 中使用 javascript 变量。
在 JavaScript 验证中使用 ajax,如果数据库中已存在用户名,则会抛出警告消息。
作为参考,请查看此链接
http://www.9lessons.info/2008/12 /twitter-used-jquery-plug-in.html
和
http://web.enavu.com/tutorials/检查用户名可用性与 ajax-using-jquery/
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.
For reference check out this link
http://www.9lessons.info/2008/12/twitter-used-jquery-plug-in.html
and
http://web.enavu.com/tutorials/checking-username-availability-with-ajax-using-jquery/
您可以通过使用 ajax 来实现这一点。以下示例使用 JQuery 及其 ajax 函数。
服务器端脚本必须由您编写,并且应该根据您的数据库检查提供的用户名。
返回值取决于您的服务器端脚本,并且可能正好相反。你可以返回任何你想要的东西。
You can achieve that by using ajax. The following example makes use of JQuery and its ajax function.
The serverside script has to be written by you and should check the provided username against your database.
The returnvalue depends on your serverside-script and may be just the other way round. You can return whatever you want.