我有脚本,检查用户是否可以注册:
PHP:
<?php
if(isset($_POST['username']))//If a username has been submitted
{
$username = mysql_real_escape_string($_POST['username']);//Some clean up :)
$check_for_username = mysql_query("SELECT * FROM users WHERE username='$username'");
//Query to check if username is available or not
if(mysql_num_rows($check_for_username))
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - Username is available
}
}
?>
JAVASCRIPT:
<script>
$(document).ready(function()//When the dom is ready
{
$("#username").change(function()
{ //if theres a change in the username textbox
var username = $("#username").val();//Get the value in the username textbox
if(username.length > 3)//if the lenght greater than 3 characters
{
$("#availability_status").html('Checking availability...');
//Add a loading image in the span id="availability_status"
$.ajax({ //Make the Ajax Request
type: "POST",
url: "http://mywebsite.com/auth/sign_up",
data: "username="+ username, //data
success: function(server_response){
$("#availability_status").ajaxComplete(function(event, request){
if(server_response == '0')//if ajax_check_username.php return value "0"
{
$("#availability_status").html('<font color="Green"> Available </font> ');
//add this image to the span with id "availability_status"
}
else if(server_response == '1')//if it returns "1"
{
$("#availability_status").html('<font color="red">Not Available </font>');
}
});
}
});
}
else
{
$("#availability_status").html('Username too short');
//if in case the username is less than or equal 3 characters only
}
return false;
});
});
</script>
但是当我写入用户名字段时,我遇到了 firebug 错误: 不允许您请求的操作。 [14:32:30.980] 发布 http://mywebsite.com/auth/sign_up [HTTP/1.1 500 内部服务器错误 63ms]
有什么建议吗?
抱歉我的英语不好:)
最佳答案
由于多种原因,这绝对不是这样做的方法
- 不推荐使用 mysql_ 扩展使用 PDO
- 如果您使用的是 codeigniter,则使用 Active Record 类进行数据库调用,使用输入类获取 POST 变量
但是我想演示可能的流程
php
$response = array(); if(isset($_POST['username'])) //If a username has been submitted { //assume username is not available $status = false; $statusText = "Not Available"; $username = mysql_real_escape_string($_POST['username']); //Some clean up :) //Query to check if username is available or not $isAvailable = mysql_query("SELECT * FROM users WHERE username='$username'"); if(mysql_num_rows($isAvailable)) { $status = true; $statusText = "Available"; } $response = array( "status" => $status, "statusText" => $statusText ); } return json_encode($response);
js
//alex >> comments usually go on top of the line you are commenting plus //you dont have to comment every single line only when something 'big' or 'strange' happens //When the dom is ready $(document).ready(function() { //if there is a change in the username textbox $("#username").change(function() { //Get the value in the username textbox var username = $("#username").val(); //alex >> caching the div so i dont call it every time which improves performance var statusDiv = $("#availability_status"); //if the length greater than 3 characters if(username.length > 3) { //TODO Add a loading image in the span id="availability_status" statusDiv.html('Checking availability...'); $.ajax({ type : "POST", url : "http://mywebsite.com/auth/sign_up", data : "username=" + username, success : function(server_response) { var color = "red"; var statusText = "an error occurred !"; server_response = JSON.parse(server_response); if(server_response) { statusText = server_response.statusText; if(server_response.status) color = "green"; } statusDiv.html("span class='" + color + "'" + statusText + "span"); } }); } else { //if in case the username is less than or equal 3 characters only statusDiv.html('Username too short'); } return false; }); });
只需记住将 span 设为适当的标签,因为“智能”编辑器正在杀死整个标签
statusDiv.html("<span class='" + color + "'>" + statusText + "</span>");
这让我想起了!
始终检查是否为空:P
关于javascript - Codeigniter JavaScript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19979024/