如何在if语句中将字体颜色从红色更改为绿色

时间:2012-08-24 22:47:35

标签: php html css

我有一个包含以下ID的表行:

  <td id='errormsg'>$errormsg</td>

在css页面中,`#errormsg'id将文本颜色变为红色,如下面单独的css文件中所示:

#errormsg{
    color:red;  
}

但我有一点情况。我在这里有一个if语句:

if(mail($getemail, $subject, $message, $headers)){
$errormsg = "You have been Registered. You must Activate your Account from the Activation Link sent to <b>$getemail</b>";
                                                }

现在我想要发生的是,如果满足此if语句,我希望$ errormsg将字体颜色从红色更改为绿色。这实际上是可能的吗?

5 个答案:

答案 0 :(得分:1)

试试这个:

$color = "red";

if(mail($getemail, $subject, $message, $headers)){
    $color = "green";
    $errormsg = "You have been Registered. You must Activate your Account from the Activation Link sent to <b>$getemail</b>";
}
....
echo "<td style='color:$color;' id='errormsg'>$errormsg</td>";

答案 1 :(得分:1)

<?php
$extraErrorClass = '';
if(mail($getemail, $subject, $message, $headers)) {
    $errormsg = "You have been Registered. You must Activate your Account from the Activation Link sent to <b>$getemail</b>";
    $extraErrorClass = ' class="error-green"';
}

echo "<td id='errormsg'$extraErrorClass>$errormsg</td>";
?>

或者更好的可读性:

echo '<td id="errormsg"', $extraErrorClass, '>', $errormsg, '</td>';

只需将额外的类添加到CSS中:

#errormsg.error-green {
    color: green;  
}

答案 2 :(得分:0)

if (mail($getemail, $subject, $message, $headers)) {
  $errormsg = "<span style='color: green'>You have been Registered. You must Activate your Account from the Activation Link sent to <b>$getemail</b></span>";
}

答案 3 :(得分:0)

<强> PHP:

<?php
// ...
$result = mail($getemail, $subject, $message, $headers);
if($result) {
?>
<div class="message message-success">You have been registered. Please activate your account from the activation link sent to <strong><?php echo $getemail; ?></strong>.
<?php
} else {
?>
<div class="message message-fail">Sorry, an error occurred when sending the activation email to <strong><?php echo $getemail; ?></strong>.
<?php
}
// ...
?>

<强> CSS:

.message {
    text-align: center;
    padding: 20px;
    border: 1px solid #888
}
.message-success {
    background: #afa;
    color: #040
}
.message-fail {
    background: #faa;
    color: #400
}

答案 4 :(得分:0)

#errormsg{
    color:red;  
}

#okmsg{
    color:green;  
}
  $status = "errormsg";
  if(mail($getemail, $subject, $message, $headers)){
    $msg = "You have been Registered. You must Activate your Account from the Activation Link sent to <b>$getemail</b>";
    $status = "okmsg";
  } 
<td id="$status">$msg</td>