2012-10-17 48 views
1

我想檢查我的mysql數據庫,如果用戶正確輸入用戶名和密碼,然後進入home.html,我有一些JavaScript代碼,檢測用戶何時單擊登錄按鈕,但我不明白如何檢查數據庫與JavaScript,我知道如何在PHP中,但我不明白如何從JavaScript調用PHP或直接在JS代碼,這是一些代碼:使用javascript檢查MySql中的用戶驗證

在HTML表單:

<div id="password"> 
     <div class="input username"><input type="text" placeholder="User name" /></div> 
     <div class="input password"><input type="password" placeholder="Password" /></div> 
     <button>Log in</button> 
     <a href="#" class="back">Back</a> 
    </div> 
</div> 

然後在我的Login.js文件我有這樣的:

function forgot() { 
//Se ho scritto la password allora fai l'animazione wobble, altrimenti vai in home.html 
    if($("#password .input.password input").attr("value")!='') { 
     $.notification( 
      { 
       title: "Wrong password", 
       content: "Just leave the password field empty to log in.", 
       icon: "!" 
      } 
     ); 
     $("#password").removeClass().addClass("animated wobble").delay(1000).queue(function(){ 
      $(this).removeClass(); 
      $(this).clearQueue(); 
     }); 
     $("#password .input.password input").attr("value", "").focus(); 
    } else { 
     document.location.href = "home.html"; 
    } 
} 

現在的登錄我只檢測,如果該字段是空的,但我想檢測mysql數據庫如何檢查它?因爲我想如果用戶輸入錯誤的用戶名和密碼跨越$通知警報,任何人都可以幫助我?

+0

使用Ajax。如果您使用jQuery,則使用Google jQuery ajax()函數。 – bogatyrjov

+0

感謝提示,我必須把jQuery?在.js文件中? – Piero

+0

要麼將​​其放入.js文件中,並使用

2

您不能直接從客戶端JS通信到服務器端數據庫。

要發出HTTP請求(到服務器端程序),請使用the XMLHttpRequest object

+0

我會建議使用JQuery的ajax()函數在幾年內沒有手動觸及XMLHttpRequest對象 –

+0

我必須在JavaScript中添加jQuery? – Piero

0
if(document.pressed=='SUBMIT') 
    { 
     var uname = document.getElementById('uname').value; 

     if(uname.trim()=='' || uname.trim()==null) 
     { 
      alert("<?php echo 'uname_null'?>"); 
      return false; 
     } 

} 
0

如果您使用存儲過程,您可以在sql server端執行驗證部分