du1108 2017-05-29 16:04
浏览 63
已采纳

尝试从警报更改为弹出窗口[重复]

This question already has an answer here:

I'm trying to switch the "success/fail" notifications to my webpage. I've been successful doing this in several parts of my test website, but I'm running into a bit of a problem on my login page. My original way of doing this used an alert popup, which works okay, but doesn't provide the style I'm looking for. I decided to use the template that has been working for me in other parts of the website, but the login is unique since it's here where I establish my session for a user.

Here is my original login code which works as intended but uses a generic alert window...

<?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);
?>

Here is the code I'm trying to get to work but comes up with

Parse error: syntax error, unexpected end of file on line 38.... which is my ?> to close out the 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);
?>

I'm pretty sure this has to do with the quotes but I've tried several different combinations and I still get the error.

The window.open code works great on my other pages if I can keep all the if, else statements within the javascript. In these pages I just use the PHP tags to grab the parameters outside the javascript where needed.

However when I attempt to do with this with the $_Session, it doesn't work.

If this is a quotes problem, I'd appreciate it if someone could point me in the right direction. If this is related to the session, I could use some help formatting the javascript so I call the ?_Session properly.

</div>
  • 写回答

2条回答 默认 最新

  • duanci5913 2017-05-29 16:13
    关注

    There are so many quote issues with your code, try to put script separately or use heredoc, nowdoc.

    PHP can read multiple lines with heredoc/nowdoc.

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

    Use delimiters and indentation correctly and you can put actual JS code in between. Example as per your use case.

    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;
    

    Remember you can not use same kind of quote in between without escaping properly but you can also double between single and vice-versa.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了