php - 在不重复表格样式的情况下回显具有可变文本(取决于条件)的表格样式

标签 php css

所以,我正在尝试回显错误警告,例如(密码/电子邮件不正确),(填写所有字段)具有相同的样式,但文本不同,但我似乎无法找到一个快捷方式来执行此操作而不回显整个在每个 echo 中使用 schpiel 风格,我相信这对大多数人来说是非常明显的,所以请帮助我。 TVM .heres 代码:

   if ($oldpassword!==$oldpassworddb)
     { echo"<head>

<style type='text/css'>
.tab2

{
   width:400px; height:40px;
   position: absolute; right: 300px; top: 70px;
}
 .td2
{
    background-color:pink;
    color:blue;
    text-align:center;
}

</style>
</head>
<body>
<table class='tab2'>
<td class='td2'>first meggage</td>
</table>
</body>";}

else if (strlen($newpassword)>25||strlen($newpassword)<6)
   {echo "what should I put in here!!! ">second message;}

最佳答案

您的处理方式不正确。避免混合使用 PHP 逻辑和输出 HTML。

确定在输出任何内容之前先显示哪条消息,并将其存储在变量中。然后输出所有带有变量的 HTML。这允许您提前定义您需要的任何其他变量,并将它们同时插入到输出中。

<?php
// First define the $message variable
$message = "";
if ($oldpassword!==$oldpassworddb) {
  $message = "first message";
}
else if (strlen($newpassword)>25||strlen($newpassword)<6) {
  $message = "Second message";
}
else {
  // some other message or whatever...
}
Close the <?php tag so you can output HTML directly
?>

然后输出 HTML(不要忘记 DOCTYPE!)

<!DOCTYPE html>
<head>

<style type='text/css'>
.tab2

{
   width:400px; height:40px;
   position: absolute; right: 300px; top: 70px;
}
 .td2
{
    background-color:pink;
    color:blue;
    text-align:center;
}

</style>
</head>
<body>
<table class='tab2'>
<!-- the PHP variable is inserted here, using htmlspecialchars() in case it contains <>&, etc -->
<td class='td2'><?php echo htmlspecialchars($message); ?></td>
</table>
</body>

使用 <table> 不被认为是一种好的现代做法对于布局,最好将 CSS 移动到通过 <link rel='stylesheet' src='yourcss.css'> 链接的外部 .css 文件中<head> 中的标签,但您应该首先解决您的 PHP 问题。

关于php - 在不重复表格样式的情况下回显具有可变文本(取决于条件)的表格样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11547230/

相关文章:

php - 使用 PHP 将分钟数转换为小时和分钟

html - Css 内容图像未显示在 Firefox 上

html - 为什么位置: relative make the height to zero?

javascript - 如何根据视口(viewport)宽度/高度计算移动背景图像的量

php - 数据库插入随机未使用的行 - 带事务

javascript - 从ajax调用获取字符串版本

jquery - CSS 或 jQuery 选择器匹配所有不是其他元素后代的后代

html - 样式化输入 type=number

php - 将大于 1MB/iOS 的文件上传到服务器

php - 如何使用 OAUTH 2 验证受信任的应用程序