检查用户名存在于数据库与AJAX [英] Check if username exists in database with AJAX
问题描述
因此,基本上,我想将功能添加到我的登记表,我会检查,如果该用户名已经存在于数据库中。
So basically, I want to add a feature to my registration form which I will check if that username already exists in the database.
我有关于AJAX的几个问题 -
I have a few questions about AJAX -
1)我想创建一个AJAX请求on_change功能,所以像这样 -
1) I want to create an AJAX request on_change function, so something like this -
$('#username').change(function() {
$.ajax({
url: "validation.php"
});
});
所以,据我了解,我必须做在PHP中validation.php文件中的所有验证,正确吗?有没有需要任何特殊的验证还是可以只是简单的验证和SQL语句 - SELECT * FROM'用户'WHERE'用户名'='。 $ _ POST [用户名];
2),所以我的理解,我必须通过$就通过POST值太大,是否正确?如果是的话,我将如何能够通过validation.php文件来访问它们?
2) So as I understood I must pass the POST values via $.ajax too, correct? If yes, how will I be able to access them via the validation.php file?
3)后,我得到的结果validation.php文件,我怎么能传回(真或假的 - 的存在或不存在)?我需要将它们传回,然后如果检查做了,如果这是真的 - ?显示的用户名已经存在一个错误,否则,不显示任何东西。
3) After I get the results in validation.php file, how can I pass them back (true or false -- exists or doesn't exist)? I will need to pass them back, and then do an if check, if it's true - show an error that the username already exists, otherwise, don't show anything?
希望,你明白我要创建。我知道有很多教程在互联网上,但我不喜欢创造,通过教程的东西,因为如果我通过自己创建它,它会更容易理解接下来的时间;!)
Hope, you understood what I'm about to create. I know there are a lot of tutorials over the internet, but I don't like to create something via tutorials, since if I create it by myself, it will be easier to understand next time ;)!
编辑:另外,我创建自己的网站以codeIgniter框架,因此,我将如何通过url属性在阿贾克斯,在codeIgniter模块
In addition, I'm creating my website with CodeIgniter framework, so how would I pass the url attribute in ajax, to a CodeIgniter module?
推荐答案
在继续, SELECT * FROM'用户'WHERE'用户名'='。 $ _ POST [用户名];
只是要求一个SQL注入。我建议你使用PHP数据对象 - http://php.net/manual/en/book.pdo.php 。
Before continuing, SELECT * FROM 'users' WHERE 'username' = '. $_POST['username'];
is just ASKING for a SQL Injection. I suggest you use PHP Data objects - http://php.net/manual/en/book.pdo.php.
所以我的理解,我必须通过$就通过POST值太大,是否正确?如果是的话,我将如何能够通过validation.php文件来访问它们?
由于这是一个简单的要求,我建议你使用jQuery的方法 $。员额()
。下面是一个示例基于关闭你正在尝试做的事情。
Because this is a simple request, I suggest you use JQuery's method $.post()
. Here's a sample based off of what you're trying to do.
$.post('validation.php',{username: $('#username').val()}, function(data){
if(data.exists){
//tell user that the username already exists
}else{
//username doesn't exist, do what you need to do
}
}, 'JSON');
jQuery的POST方法需要4个参数 $。员额(网址,数据,回调,数据类型)
。在上面的例子中,我们将发布与 $('#用户名)的用户名。VAL()
到 validation.php
并期望 JSON
响应。当请求完成后,回调函数将与数据执行
是从请求的响应。因为我们指定的响应将是 JSON
,我们可以访问它,就像在JavaScript原生对象。现在,让我们看一下validation.php
jQuery's post method takes 4 parameters $.post(url, data, callback, datatype)
. In the example above, we will be posting the username with $('#username').val()
to validation.php
and expect a JSON
response. When the request is finished, the callback function will be executed with data
being the response from the request. Because we specified that that response will be JSON
, we can access it just like a native object in javascript. Now let's move to validation.php
就像我上文所述,我建议你使用PDO的数据库驱动程序。因此,在这个例子中,我会告诉你它的基本用法。
Like I stated above, I suggested you use PDO for your database driver. So in this example, I will show you a basic usage of it.
//set the headers to be a json string
header('content-type: text/json');
//no need to continue if there is no value in the POST username
if(!isset($_POST['username']))
exit;
//initialize our PDO class. You will need to replace your database credentials respectively
$db = new PDO('mysql:host=DATABASE_HOST;dbname=DATABASE_NAME','DATABASE_USERNAME','DATABASE_PASSWORD',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
//prepare our query.
$query = $db->prepare('SELECT * FROM users WHERE username = :name');
//let PDO bind the username into the query, and prevent any SQL injection attempts.
$query->bindParam(':name', $_POST['username']);
//execute the query
$query->execute();
//return the json object containing the result of if the username exists or not. The $.post in our jquery will access it.
echo json_encode(array('exists' => $query->rowCount() > 0));
现在回顾一下,我们的jQuery脚本将张贴到 validation.php
它从数据库中选择一个用户名。它会返回一个 JSON
对象,它具有的项存在
这是一个布尔值,表示如果该用户名已经存在,作为一个排在你的数据库。当请求完成后通过jQuery,您可以您所需要的基础关闭查询结果做的。我希望这是一个彻底的解释,并引导你到你希望什么来完成。
Now to recap, our jQuery script will post to validation.php
where it selects a username from the database. It will return a JSON
object that has a key of exists
that is a boolean indicating if the username already exists as a row in your database. When the request is complete via jQuery, you can do what you need based off the result of the query. I hope this is a thorough explanation and leads you to what you are hoping to accomplish.
这篇关于检查用户名存在于数据库与AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!