2017-05-29 139 views
1

我想「成功/失敗」通知切換到我的網頁。我已經在我的測試網站的幾個部分成功完成了這個任務,但是我在登錄頁面遇到了一些問題。我原來的做法是使用一個警告彈出窗口,它可以正常工作,但不提供我正在尋找的樣式。我決定在網站的其他部分使用一直爲我工作的模板,但登錄是獨一無二的,因爲它是我爲用戶建立會話的地方。試圖改變從警報彈出窗口

這是我原來的登錄代碼,按預期工作,但使用一個通用的警告窗口...

<?php 
session_start(); 
require_once '../php/connect.php'; 

if (isset($_POST['username']) and isset($_POST['password'])){ 

    $username = mysqli_real_escape_string($link, $_POST['username']); 
    $password = mysqli_real_escape_string($link, $_POST['password']); 

$result = mysqli_query($link, "SELECT * FROM planner WHERE username = '$username' and password = '$password'"); 
$count = mysqli_num_rows($result); 

if ($count !== 1){ 
    echo "<script> window.location.href='../default.html'; alert('Your credentials could not be validated!')</script>"; 
    } else { 
     $_SESSION['username'] = $username; 
    } 

if (isset($_SESSION['username'])){ 
    header("Location: ../php/main.php"); 
    } else { 
     echo "<script> window.location.href='../default.html'; alert('Your credentials could not be validated!')</script>"; 
    } 
} 
mysqli_close($link); 
?> 

這裏是我想要去工作的代碼,但與

出現解析錯誤:語法錯誤,第38行文件意外結束....這是我的?>關閉了PHP。

<?php 
session_start(); 
require_once '../php/connect.php'; 

if (isset($_POST['username']) and isset($_POST['password'])){ 

$username = mysqli_real_escape_string($link, $_POST['username']); 
$password = mysqli_real_escape_string($link, $_POST['password']); 

$result = mysqli_query($link, "SELECT * FROM planner WHERE username = '$username' and password = '$password'"); 
$count = mysqli_num_rows($result); 

if ($count !== 1){ 
echo "<script> 
var no = window.open('', 'failure','top=250,left=500,height=200,width=350,menubar=no,scrollbars=no,toolbar=no'); 
no.document.write('<body bgcolor='#EFEFEF'/>'); 
no.document.write('</br>'); 
no.document.write('<p style='text-align:center;color:white;background-color:red;font-family:Helvetica;font-size:20px'>Your credentials could not be verified</p></br>'); 
no.document.write('<div style='text-align:center'><button style='width:100px;border-style:solid;border-width:5px;border-color:#003399;position:absolute;left:35%;background-color:#003399;color:#ffcc00;font-weight:bold;font-family:Helvetica' value='Close' onclick='window.close()'>OK</button></div>'); 
window.location.href = '../default.html';</script>"; 
} else { 
    $_SESSION['username'] = $username; 
} 

if (isset($_SESSION['username'])){ 
header("Location: ../php/main.php"); 
} else { 
echo "<script> 
var no = window.open('', 'failure','top=250,left=500,height=200,width=350,menubar=no,scrollbars=no,toolbar=no'); 
no.document.write('<body bgcolor='#EFEFEF'/>'); 
no.document.write('</br>'); 
no.document.write('<p style='text-align:center;color:white;background-color:red;font-family:Helvetica;font-size:20px'>Your credentials could not be verified</p></br>'); 
no.document.write('<div style='text-align:center'><button style='width:100px;border-style:solid;border-width:5px;border-color:#003399;position:absolute;left:35%;background-color:#003399;color:#ffcc00;font-weight:bold;font-family:Helvetica' value='Close' onclick='window.close()'>OK</button></div>'); 
window.location.href = '../default.html';</script>"; 
} 

mysqli_close($link); 
?> 

我很確定這與引號有關,但我嘗試了幾種不同的組合,但仍然出現錯誤。

的window.open代碼工作在我的其他網頁巨大的,如果我能保持在JavaScript中所有的如果,else語句。在這些頁面中,我只是使用PHP標籤在需要的地方獲取javascript之外的參數。

然而,當我試圖用這個與$ _SESSION做,這是行不通的。

如果這是一個引號的問題,我會很感激,如果有人可以在正確的方向指向我。如果這與會話有關,我可以使用一些格式化JavaScript的幫助,以便正確調用?_Session。

+0

好像你缺少一個右}爲你的第一個'if' – RST

+0

這個錯誤意味着有一個丟失的大括號,數你的大括號 –

+0

Do'h ....以及刪除錯誤信息。現在我只是一個空白的login.php屏幕... – airider74

回答

2

有你的代碼這麼多的報價問題,儘量把單獨腳本或用定界符,nowdoc。

PHP可以讀取多個線路用定界符/ nowdoc。

使用分隔符和縮進正確,你可以把實際的JS代碼之間。 根據您的使用情況舉例。

echo <<<SCRIPT 
<script> 
var no = window.open('', 'failure','top=250,left=500,height=200,width=350,menubar=no,scrollbars=no,toolbar=no'); 
no.document.write('<body bgcolor="#EFEFEF"/>'); 
no.document.write('</br>'); 
no.document.write('<p style="text-align:center;color:white;background-color:red;font-family:Helvetica;font-size:20px">Your credentials could not be verified</p></br>'); 
no.document.write('<div style="text-align:center"><button style="width:100px;border-style:solid;border-width:5px;border-color:#003399;position:absolute;left:35%;background-color:#003399;color:#ffcc00;font-weight:bold;font-family:Helvetica" value="Close" onclick="window.close()"">OK</button></div>'); 
window.location.href = '../default.html'; 
</script> 
SCRIPT; 

記住,你不能用同一種引號之間沒有逃脫正常,但你也可以單間,反之亦然一倍。

+0

是的,我很喜歡這些線,因爲我碰到了其他地方,但能夠調整我的代碼,以保持一切對齊...我會給這個去... – airider74

+0

這做了伎倆....再次感謝您的幫助......像冠軍一樣奔跑。 – airider74

1

我覺得你的問題是使用「內的另一個」

no.document.write('<p style='text-align:center;color:white;background-color:red;font-family:Helvetica;font-size:20px'>... 

你必須逃離這個字符是這樣的:

no.document.write('<p style=\'text-align:center;color:white;background-color:red;font-family:Helvetica;font-size:20px\'>... 
+0

謝謝Jardel ...欣賞洞察力,並幫助保持我的報價對齊和格式化字符對齊... – airider74

+0

我很高興爲您提供幫助! –