我的验证脚本的一部分根据错误吐出错误代码。它包括显示一个html错误页面。在错误页面上打印或回显的语句总是向下推页面,而且似乎还会影响一些奇怪的东西,如字体大小等。默认情况下,它总是在页面顶部的中心显示回显语句。有没有办法让我做这个?我希望错误消息显示在html中的某个区域?
if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
echo 'You did not submit the following required information! <br />';
if(!$first_name){
echo "First Name is a required field. Please enter it below.<br />";
}
if(!$last_name){
echo "Last Name is a required field. Please enter it below.<br />";
}
if(!$email_address){
echo "Email Address is a required field. Please enter it below.<br />";
}
include 'error_reg.htm';
exit();
}
请在php中执行以下操作:
echo "<p class='my_error'>Email Address is a required field. Please enter it below.</p>";
然后在外部.css文件中(或在页面头部的样式标签中):
.my_error{
color: red;
font-size: 14pt;
/*... and whatever you want*/
}
最简单的方法(然而,被认为是一种糟糕的做法)是使用内联样式,如:
echo "<p style='color:#FF0; font-size:14pt'>Some message</p>";
请只在应用程序非常小的情况下使用这种方法,而样式和html的混合会使维护变得非常复杂。
事实上,它非常简单,在您的echo中,只需在该消息上添加/包装您想要的html标记。造型可以是任何你想要的样子。这个错误的位置也完全取决于你。
这里有一个非常基本的例子:
<!-- or just use an external css file -->
<style type="text/css">
.error-box {
color:#555;
font-family:Tahoma,Geneva,Arial,sans-serif;font-size:11px;
padding:10px 10px 10px 36px;
margin:10px;
background-color: #ffecec;
border:1px solid #f5aca6;
width: 300px;
}
</style>
<?php
$first_name = '';
if(!$first_name){
echo "<div class='error-box'>First Name is a required field. Please enter it below.</div>";
}
?>
这根本就不是很多工作。
echo "<p class="whatever">;
echo "your error messages";
echo "</p>";
你基本上只是用html标签重新封装错误消息,这样你就可以使用带有的css来对其进行样式设置
whatever {
font-size: 12px
}
您可以通过在HTML中添加PHP脚本来实现这一点。我不知道你的error_reg.htm文件里有什么。
<body>
<div id="error_status">
<?php
if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
echo 'You did not submit the following required information! <br />';
if(!$first_name){
echo "First Name is a required field. Please enter it below.<br />";
}
if(!$last_name){
echo "Last Name is a required field. Please enter it below.<br />";
}
if(!$email_address){
echo "Email Address is a required field. Please enter it below.<br />";
}
include 'error_reg.htm';
exit();
}
?>
</div>
</body>