スクリプトを取得しました。ユーザーが登録可能かどうかを確認します。
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
}
}
?>
ジャバスクリプト:
<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] POST http://mywebsite.com/auth/sign_up [HTTP/1.1 500 内部サーバー エラー 63 ミリ秒]
なにか提案を?
私の悪い英語でごめんなさい:)