尝试从警报更改为弹出窗口

时间:2017-05-29 16:04:52

标签: javascript php session

我正在尝试将“成功/失败”通知切换到我的网页。我已经在我的测试网站的几个部分成功完成了这项工作,但我在登录页面上遇到了一些问题。我这样做的原始方式使用了一个警告弹出窗口,它可以正常工作,但不提供我正在寻找的样式。我决定使用在网站的其他部分为我工作的模板,但是登录是唯一的,因为我在这里为用户建立会话。

这是我的原始登录代码,它按预期工作,但使用通用警报窗口...

<?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行意外的文件结尾....这是我的?&gt;关闭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);
?>

我很确定这与引号有关,但我尝试了几种不同的组合,但我仍然遇到错误。

如果我可以在javascript中保留所有if,else语句,window.open代码在我的其他页面上运行得很好。在这些页面中,我只是使用PHP标记来获取所需的javascript之外的参数。

但是,当我尝试使用$ _Session执行此操作时,它不起作用。

如果这是一个报价问题,如果有人能指出我正确的方向,我会很感激。如果这与会话有关,我可以使用一些帮助格式化javascript,所以我正确地调用了?_Session。

2 个答案:

答案 0 :(得分:2)

您的代码有很多引用问题,请尝试单独放置脚本或使用heredoc,nowdoc。

PHP可以使用heredoc / nowdoc读取多行。

echo <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

正确使用分隔符和缩进,您可以在其间放置实际的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;

请记住,如果没有正确转义,你不能在两者之间使用相同类型的引用,但你也可以在单一之间加倍,反之亦然。

答案 1 :(得分:1)

我认为您的问题正在使用&#39;在另一个&#39;

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\'>...