2010-09-03 54 views
1

我創建登錄時,點擊提交按鈕發送變量login_success.php page.but我想使當我點擊提交按鈕登錄表單將關閉。我可以使用jquery關閉表格隱藏登錄表單與Jquery

<script type="text/javascript"> 
$(document).ready(function(){ 
$("button").click(function(){ 
    $(".loginform").hide(); 
}); 
}); 
</script> 

但是這個時候表單不發送請求到.php文件。我將它製作成.php文件的插件腳本,然後重定向到index.html site.It也很好,但是我可以看到反射。如何將它們合併?

這是我的形式

<div class="loginform"> 
    <form action="php/login.php" method="post" id="login"> 

     <fieldset class="loginfield"> 
        <div> 
      <label for="username">User Name</label> <input type="text" id="username" name="username"> 
     </div> 
     <div> 
      <label for="password">Password</label> <input type="password" id="password" name="password"> 
     </div> 
    </fieldset> 
     <button type="submit" id="submit-go" ></button> 
    </form> 
</div> 

編輯 我使用的功能函數naveed傷心。我在Firefox安裝螢火,我可以看到我的表單驗證工作normal.It發送和請求的login.php但我無法對我的表格做任何更改。它不會關閉或$ div值不顯示在div標籤上。

回答

1

您應該使用JSON/AJAX組合:

Downlod jQuery

如果您的形式是這樣的:

<script type="text/javascript" src="jquery-1.4.2.js"></script> 
<script type="text/javascript" src="ajax.js"></script> 

<div id='errors'></div> 
<div class='loginform' id='loginform'> 
    <form action="php/login.php" method="post" id="login"> 
    Username:<input type="text" id="username" name="username"> 
    Password:<input type="password" id="password" name="password"> 
    <button type="submit" id="submit-go" value='Login'></button> 
    </form> 
</div> 

jQuery代碼在ajax.js文件提交表單,然後從'php/login.php'在JSON並填寫所需的DIV。如果登錄是窗體的ID。

jQuery('#login').live('submit',function(event) { 
    $.ajax({ 
     url: 'php/login.php', 
     type: 'POST', 
     dataType: 'json', 
     data: $('#login').serialize(), 
     success: function(data) { 
      for(var id in data) { 
       jQuery('#' + id).html(data[id]); 
      } 
     } 
    }); 
    return false; 
}); 

的login.php在形式上action屬性描述文件:

$username = $_POST['username']; 
$password = $_POST['password']; 

if($username and $password found in database) { 

    // It will replace only id='loginform' DIV content 
    // and login form will disappear 
    $arr = array ("loginform" => "you are logged in"); 

} else { 

    // It will replace only id='errors' DIV content 
    $arr = array ("errors" => "You are not authenticated. Please try again"); 

} 
echo json_encode($arr); 



更多詳細信息:

+0

我試過這個,但是當我點擊提交沒有任何反應。對於使用ajax,我應該使用一些插件或jquery有其?並且在表單標籤中我應該刪除action =「php/login」? – Ercan 2010-09-04 09:55:30

+0

您必須下載jQuery,並將其包含在您的表單文件中。如果你的form標籤中有'action =「php/login」',那麼你可以在'$ .ajax'函數中使用'url:$(this).attr('action')'。看看答案的變化。 – NAVEED 2010-09-05 17:08:15

+0

可能是你錯過了一些東西。確保你的jquery文件包含在正確路徑的表單文件中。如果你的jQuery代碼在單獨的文件中,那麼確保它包含在表單文件中。檢查在jQuery中使用的表單的ID來捕獲事件。 – NAVEED 2010-09-05 17:20:34

0

嘗試submit method

$("button").click(function(){ 
    $("form.loginform").submit().hide(); 
}); 

PS你知道,在頁面上應用onclick處理所有<button>元素是壞主意,對吧?

+0

我試過,但它也重定向到PHP文件 – Ercan 2010-09-03 23:39:33

0
$(document).ready(function(){ 
    $("button").click(function(){ 
     $(".loginform").hide(); 
      return false; 
    }); 
}); 
+0

它關閉時,我輸錯通或用戶名登錄表單。如果我輸入ture登錄結束密碼,它會將我重定向到login_success.php。我怎樣才能讓它成爲明智的詩句?或者錯誤的時候,它只會顯示在你沒有註冊的頁面上。 – Ercan 2010-09-04 00:00:25