Question

So I have a form that has some custom made alerts created combined in CSS and PHP, now I call these alerts using echo and I use die() at the end to kill/end the PHP script. Here is the PHP code:

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

and here is the 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;
        }

The problem is when the script is executed without die() then is shows the error properly with the CSS. When die() is implemented then the code shows a blank page that says "Passwords do not match!" without any styling.

What can I do to kill the PHP script but still have styling?

SOLUTION:

So after seeing the answers and realizing that die kills the entire site at that point. I added a simple line to my code:

BEFORE:

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

AFTER:

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

So basically I appended just the stylesheet before the alert to make the alert have styling.

Était-ce utile?

La solution

In order for this to work with the method you're presently using, you would need to have your HTML on top of PHP.

I tested this with both exit('<div class="alert">Passwords do not match!</div>'); and die('<div class="alert">Passwords do not match!</div>');

<!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

}
?>

You could do it this way, using an additional conditional statement, instead of killing the script:

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

Autres conseils

Most likly your css file is added later in the code. When executing a die(); statement php will stop all further processing. Effektivly not echoing the stylesheet.

If you look in your the source of the "blank page" you'll see that it contains:

 '<div class="alert">Passwords do not match!</div>'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top