【问题标题】:echoing <div class> not working when using PHP die();使用 PHP die() 时回显 <div class> 不起作用;
【发布时间】:2014-03-14 07:04:13
【问题描述】:

所以我有一个表单,其中包含一些在 CSS 和 PHP 中组合创建的自定义警报,现在我使用 echo 调用这些警报,并在最后使用 die() 来终止/结束 PHP 脚本。这是PHP代码:

if($pass1 != $pass2){
echo '<div class="alert">Passwords do not match!</div>';
die();
}

这里是 CSS:

.alert {
        background: #fff6bf url(../images/exclamation.png) center no-repeat;
        background-position: 15px 50%; 
        text-align: center;
        padding: 5px 20px 5px 45px;
        border-top: 2px solid #ffd324;
        border-bottom: 2px solid #ffd324;
        }

问题是当脚本在没有 die() 的情况下执行时,CSS 会正确显示错误。当 die() 被实现时,代码会显示一个空白页,上面写着“密码不匹配!”没有任何造型。

我可以做些什么来杀死 PHP 脚本但仍然有样式?

解决方案:

因此,在看到答案并意识到此时死亡会杀死整个站点之后。我在代码中添加了一行简单的代码:

之前:

   if($pass1 != $pass2){
    echo '<div class="alert">Passwords do not match!</div>';
    die();
    }

之后:

   if($pass1 != $pass2){
    echo '<link rel="stylesheet" type="text/css" href="css/structure.css">';
    echo '<div class="alert">Passwords do not match!</div>';
    die();
    }

所以基本上我只在警报之前附加了样式表,以使警报具有样式。

【问题讨论】:

  • 当你使用 die();它会导致页面在您所在的位置停止渲染。因此,如果您的 HTML 没有完成并且您“死亡”,那么它就无法呈现。例如,如果您的 css 在您死之前不在页面上,则 css 无法显示。您可以通过查看源代码来检查这一点
  • 你真的不应该使用die() 或者只是退出生产中的脚本。这听起来更像是一个程序设计问题。
  • 将错误消息存储到变量中。稍后,如果变量非空(发生错误),则显示它而不是执行在非错误状态下会发生的任何其他事情。不要突然die(),停止你的脚本。
  • 为了让它工作,你需要在 PHP 之上有你的 HTML。我用exit('&lt;div class="alert"&gt;Passwords do not match!&lt;/div&gt;');die('&lt;div class="alert"&gt;Passwords do not match!&lt;/div&gt;'); 测试了这个

标签: php css echo die


【解决方案1】:

为了使这与您当前使用的方法一起工作,您需要在 PHP 之上拥有 HTML。

我用exit('&lt;div class="alert"&gt;Passwords do not match!&lt;/div&gt;');die('&lt;div class="alert"&gt;Passwords do not match!&lt;/div&gt;'); 对此进行了测试

<!DOCTYPE html>
<head>
<style>

.alert {
        background: #fff6bf url(../images/exclamation.png) center no-repeat;
        background-position: 15px 50%; 
        text-align: center;
        padding: 5px 20px 5px 45px;
        border-top: 2px solid #ffd324;
        border-bottom: 2px solid #ffd324;
        }

</style>
</head>

<?php
$pass1="2"; // my own test variable
$pass2="3"; // my own test variable
if($pass1 != $pass2){

exit('<div class="alert">Passwords do not match!</div>');
// die('<div class="alert">Passwords do not match!</div>');

echo "Hello there"; // Will not echo

}
?>

你可以这样做,使用额外的条件语句,而不是杀死脚本:

<?php
$pass1="2"; // my own test variable
$pass2="3"; // my own test variable

$var = "<div class=\"alert\">Passwords do not match!</div>";

if($pass1 != $pass2){
echo $var;
}

else{
echo "You have access.";
}

?>

<!DOCTYPE html>

<head>
<style>

.alert {
font-family:Georgia;
color:#ffff00;

        background: #fff6bf url(../images/exclamation.png) center no-repeat;
        background-position: 15px 50%; 
        text-align: center;
        padding: 5px 20px 5px 45px;
        border-top: 2px solid #ffd324;
        border-bottom: 2px solid #ffd324;
        }

</style>
</head>

【讨论】:

    【解决方案2】:

    您的 css 文件很可能是稍后在代码中添加的。执行 die() 时;声明 php 将停止所有进一步的处理。有效地不呼应样式表。

    如果您查看“空白页”的来源,您会看到它包含:

     '<div class="alert">Passwords do not match!</div>'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 2011-12-31
      • 2021-07-17
      • 2021-08-20
      • 2013-08-26
      • 1970-01-01
      • 2020-03-24
      • 2013-05-12
      相关资源
      最近更新 更多