所以,我正在尝试回显错误警告,例如(密码/电子邮件不正确),(填写所有字段)具有相同的样式,但文本不同,但我似乎无法找到一个快捷方式来执行此操作而不回显整个在每个 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/