2014-09-04 32 views
0

嗨,大家好,我有一個以下代碼,我試圖重定向到另一個,我使用javascript html來實現這一點,我是新來的HTML,JavaScript和PHP。它顯示test.php,但當我點擊按鈕,它不會重定向到logout_success.php在html和javascript中從一個頁面到另一個頁面的重定向錯誤?

<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>Sign-In</title> <link rel="stylesheet" type="text/css" href="style-sign.css"> 
    </head> 
    <body id="body-color"> 
     <div id="Sign-In"> 
      <fieldset style="width:30%"> 
       <legend>LOG-IN HERE</legend> 
       <form method="POST" action="connectivity.php"> 
        User 
        <br> 
        <input type="text" name="user" size="40"> 
        <br> 
        Password 
        <br> 
        <input type="password" name="pass" size="40"><br> 

        <input id="button" type="submit" name="submit" value="Log-In" onclick="document.location.href='http://localhost:8080/PortalWork/logout_success.php'"> 
       </form> 
      </fieldset> 
     </div> 
    </body> 
</html> 
+1

<形式方法= 「POST」 行動= 「connectivity.php」>使用logout_success.php而不是connectivity.php – Jageen 2014-09-04 10:32:37

回答

0

你可以做到這一點的JS是這樣的:

<button name="button" onclick="window.location.href='http://www.google.com'">Go</button> 

而不是http://www.google.com使用你想要的網址。

+0

您能否給我建議一件事,現在我有'logout_success.php'在同一臺機器上,但是如果'logout_success.php'在具有相同LAN的其他機器上,我該怎麼辦? – CodeSniper 2014-09-04 11:33:02

+0

我不知道我理解你,但如果你想要在局域網中的各種PC上工作:http://stackoverflow.com/questions/10018955/how-to-access-my-localhost-from-another- PC-在蘭 – 2014-09-04 12:11:46

1

有兩種方法。

(1)提交表單與AJAX,並用window.location.href

$("#form1").submit(function(e){ 
    e.preventDefault(); 
    $.ajax({ 
     url:'connectivity.php', 
     method:'POST', 
     success:function(rs){ 
      if(rs == "success") 
       window.location.href("logout_success.php"); 
     }, 
     error:function(){ 
      alert("Error"); 
     } 
    }); 
    return false; 
}); 

http://jsfiddle.net/hxm49uk2/

(2)提交給服務器,並與報頭重定向(「位置重定向:page.php文件「);

connectivity.php

header("Location:logout_success.php"); 
+0

我試過下面的代碼,但它直接把我帶到logout_success.php而不是test.php CodeSniper 2014-09-04 10:36:48

+0

你可以請吃嗎? – CodeSniper 2014-09-04 10:37:50

+0

嘗試http://jsfiddle.net/hxm49uk2/ – Parfait 2014-09-04 10:38:59

0

只需包括在connectivity.php

header("Location:logout_success.php"); // give the correct path of logout_success.php 

下面的代碼並從形式起飛的onclick提交按鈕

<input id="button" type="submit" name="submit" value="Log-In"> 
相關問題