2013-12-23 44 views
1

我是PHP的新手,剛剛開始學習這門語言的基礎知識。綁定buttonClick()事件到PHP函數

我創建了一個頁面login.php包含文本字段和密碼字段。我想要做的是,點擊提交按鈕,我想調用我在同一頁面上定義的PHP方法authenticateUser()。如果用戶被成功驗證,那麼他將被引導至index.html頁面。如果沒有,那麼他仍然在同一頁面上,並彈出錯誤消息,說用戶名/密碼不正確。所有這些邏輯已在authenticateUser()函數中定義。出於安全目的,我沒有在這裏包含代碼。

問題是該按鈕駐留在客戶端,PHP腳本駐留在服務器端。任何人都可以請告訴我,我怎麼能做到這一點? login.php的源代碼如下。先謝謝你。

的login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Login</title> 
</head> 

<body> 
    <?php function authenticateUser($username,$password) { //Code to check the user 's credentials 
      } 
     ?> 
     <form id="form1" name="form1" method="post" action="index.php"> 
     <input name="txtUsername" type="text" id="txtUsername" size="30" maxlength="100" /> 
     <input name="txtPassword" type="password" id="txtPassword" size="30" maxlength="100" /> 
     <input type="submit" name="btnLogin" id="btnLogin" value="Log In" /> 
     </form> 
</body> 
</html> 
+0

您應該閱讀有關Ajax的文檔。 – pbenard

回答

3

既可以使用AJAX請求和分離的邏輯,或提交表單到同一頁,並做了檢查。例如;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Login</title> 
</head> 
<body> 
<?php 
    if(isset($_POST)) { 
     //Let's assume sanitation and validation on input is done in the function 
     authenticateUser($_POST['txtUsername'], $_POST['txtPassword']); 
    } 
    function authenticateUser($username,$password) 
     { 
     //Code to check the user's credentials 
    } 
?> 
<form id="form1" name="form1" method="post" action=""> 
<input name="txtUsername" type="text" id="txtUsername" size="30" maxlength="100" /> 
<input name="txtPassword" type="password" id="txtPassword" size="30" maxlength="100" /> 
<input type="submit" name="btnLogin" id="btnLogin" value="Log In" /> 
</form> 

我所做的;

  • 新增if(isset($_POST)) {,看是否有用戶發佈任何內容,然後調用函數
  • 刪除表單的action屬性值。

回答的評論下面的問題

1)我倒是更個人的喜好。另外,如果此頁面從index.php重命名,您的腳本將無法按預期工作。將其設置爲空白將發佈到當前頁面。

2)假設所有檢查都已完成,並且您只是想將它們重定向到所述頁面,則可以執行以下操作;

ob_clean(); //As we'd be sending a header() request after HTML output. 
header('Location: index.php'); 

如果authenticateUser返回布爾型TRUE你可以把這個在authenticateUser功能,或其他地方 - 這一切都取決於你的邏輯。

另外,還要確保你創建一個會話登錄的用戶,並相應地處理會話,考慮到措施來處理漏洞 - 比如會話劫持。

+0

非常感謝您的回覆。我在這裏有兩個問題。 1)爲什麼你從表單中刪除動作屬性值? 2)假設用戶是否已經成功通過身份驗證,我應該在哪裏編寫邏輯並將其重定向到index.php? – Razor

+0

我會更新我的答案來解決這些問題:) –

+0

再次感謝您爲我提供的解決方案。 – Razor

0

簡短的回答:這是不可能綁定您的提交按鈕,一個PHP函數。

原因很簡單:PHP是服務器端,和HTML/JS是客戶端。客戶端根本不會看到你的任何PHP代碼。

要解決這個問題,可以使用jQuery綁定提交按鈕來調用通過Ajax的PHP函數的函數,你會提出你的PHP代碼到一個單獨的文件。

1

您正確的方向,你不能執行點擊功能。但是,您可以在設定的動作上執行該功能,使用isset()進行測試。

if (isset($_POST['btnLogin'])) { 
    authenticateUser($_POST['txtUsername'], $_POST['txtPassword']); 
} 

如果btnLogin設置這將執行該功能,但是當您單擊按鈕,您將需要的頁面(即新的請求,該頁面將重新加載,然後它會執行此功能。

您可以使用ajax請求來簡化請求響應

但是,當您使用POST數組中的值時,不需要將它們作爲參數傳遞它們是全局作用域的一部分,所以它們對功能可見。

0

I將提交到同一頁面並檢查那裏的輸入。如果通過驗證,則重定向。

 <?php 
      if(isset($_POST)) //checks if form has been submitted 
      { 
       authenticateUser($_POST['txtUsername'],$_POST['txtPassword']); 
      } 

      function authenticateUser($username,$password) 
      { 
       //Code to check the user's credentials 
       //example: 
       if($username === null) 
       { 
       header('location:index.php'); //redirects to index.php 
       } 
      } 
     ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Login</title> 
     </head> 
     <body> 
     <form id="form1" name="form1" method="post" action="login.php"> 
     <input name="txtUsername" type="text" id="txtUsername" size="30" maxlength="100" /> 
     <input name="txtPassword" type="password" id="txtPassword" size="30" maxlength="100" /> 
     <input type="submit" name="btnLogin" id="btnLogin" value="Log In" /> 
     </form> 
    </body> 
</html> 
相關問題