2015-06-28 27 views
0

早上好,打開鏈接作爲一個正常的頁面

最近我玩弄試圖建立教育原因,簡單的聊天室。在我的登錄/註冊/歡迎屏幕上,我有一個按鈕,在iframe上打開login.php文件,以便您可以登錄。如果您將正確的用戶名和密碼存儲在數據庫中,它會將您重定向到./chat/index.php頁面(主聊天室頁面)。問題是這個頁面在iframe中打開。有沒有一種方法可以像普通頁面那樣在同一個窗口上打開,就像簡單的按鈕鏈接所做的那樣。

這是我的主網頁代碼:

<div id="login" class="menu"> 
<h4>I have an account :)</h4> 
<form method="get" action="./login" target="loginFrame"> 
    <button type="submit" onclick="showLoginFrame">Please let me in so I can talk with my friends!</button> 
</form> 
<iframe id="loginIframe" name="loginFrame" frameborder="0" scrolling="no"></iframe> 
<script type="text/javascript"> 
    function showLoginFrame() { 
    var iframe1 = document.getElementById("loginIframe"); 
    iframe1.style.visibility="visible"; 
    } 
</script> 

這是一個重定向到聊天登錄PHP頁面(即做所有的工作與DB的一個)的部分代碼頁:

if($user == $dbusername && $pass == $dbpassword) 
    { 
    session_start(); 
    $_SESSION['sess_user']=$user; 
/* Redirect browser */ 
    header("Location: ../chat/"); 
    } 
} else { 
    echo "Invalid username or password!<br><br>"; 
} 
+1

不要使用iframe的將是我的第一個消息。 「醜陋的」解決方案:在你的php中代替'header()',打印一個改變'父窗口''href'的js代碼。 –

+0

那麼我使用iframes,因爲它們很容易控制與js和表單標籤,但這傢伙的解決方案\/\/\ /工作得很好,所以thnx你的時間! –

回答

1

雖然我@Ofir巴魯克同意你應該考慮一個對話框來代替,把下面的JavaScript您./chat/index.php應該胸圍它的框架。

if (top.location!= self.location) { 
    top.location = self.location.href; 
} 

在這個不適合你的工作情況這裏有兩個其他的解決方案:

https://css-tricks.com/snippets/javascript/break-out-of-iframe/

+0

謝謝。您的js解決方案工作得很好......我將代碼放在頁面頂部的