所以,我试图用相同的样式但只是不同的文本来回显错误警告,比如ass(密码/电子邮件不正确)、(填写所有字段),但如果不在每次回显中回显整个样式,我似乎找不到捷径,我相信这对大多数人来说都是显而易见的,所以请帮助我。TVM。这里的代码:
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移动到通过<head>
中的<link rel='stylesheet' src='yourcss.css'>
标记链接的外部.CSS文件中,但您应该首先解决PHP问题。