2013-11-14 61 views
0

我得到了腳本,該檢查的用戶可以註冊:笨JavaScript錯誤

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> 

後來我寫進去用戶名字段我錯誤與螢火蟲: 您所要求的行動不是允許。 [014:32:30.980] POST http://mywebsite.com/auth/sign_up [HTTP/1.1 500內部服務器錯誤63ms]

有什麼建議嗎?

對不起我的英語不好:)

+2

如果你正在使用笨,笨使用庫訪問POST值,會話處理和數據庫操作請。 – Ramesh

+0

如果您直接將瀏覽器指向http://mywebsite.com/auth/sign_up,是否會出現相同的錯誤? –

+0

Matt,no i dont – Karambyto04

回答

1

這絕對不是這樣做的原因很多像

  1. mysql_擴展的方式已經過時使用PDO
  2. ,如果你使用的是笨然後使用活動記錄類用於進行數據庫調用和獲取POST變量的輸入類

然而,id喜歡演示可能的流程

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; 
    }); 
}); 

只是別忘了要跨越一個適當的標籤,因爲 '聰明' 的編輯是殺害整個標籤

statusDiv.html("<span class='" + color + "'>" + statusText + "</span>");

這讓我想起!

總是檢查空:P

+0

我只是想再次注意到,PHP代碼應該位於'Auth'控制器中名爲'sign_up'的方法內。 –

+0

現在看起來很完美,但它沒有工作,我得到錯誤:[15:27:43。670] TypeError:server_response爲null @ http://website.com/register/ – Karambyto04

+0

嘗試添加'else'分支到'if(isset($ _ POST ['username']))''status'設置爲false,只是爲了確保你永遠不會從服務器得到空的結果。 –