【问题标题】:Trying to change from alert to popup window [duplicate]试图从警报更改为弹出窗口[重复]
【发布时间】:2023-03-13 17:58:01
【问题描述】:

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

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

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

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

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

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

如果这是一个引号问题,如果有人能指出我正确的方向,我将不胜感激。如果这与会话有关,我可以使用一些帮助格式化 javascript,以便正确调用 ?_Session。

【问题讨论】:

  • 似乎您缺少第一个 if 的结束 }
  • 该错误意味着缺少右花括号,请数一下大括号
  • Do'h....好吧,删除了错误消息。现在我得到一个空白的 login.php 屏幕...
  • 在您的建议之间,这是我的一个面子,而 anwerjunaid 现在一切正常......谢谢大家!!!

标签: javascript php session


【解决方案1】:

你的代码引用问题太多了,尝试单独放脚本或者使用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;

请记住,如果不正确转义,您不能在两者之间使用相同类型的引号,但您也可以在单引号之间使用双引号,反之亦然。

【讨论】:

  • 是的,我是这样思考的,因为我会在其他地方遇到它,但能够调整我的代码以保持一切对齐......我会试一试。 ..
  • 这成功了....再次感谢您的帮助...像冠军一样奔跑。
【解决方案2】:

我认为你的问题是使用 ' inside another '

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

【讨论】:

  • 感谢 Jardel...感谢您的见解并帮助我保持引号对齐和格式字符对齐...
  • 我很高兴为您提供帮助!
猜你喜欢
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多