如果错误未填写表单部分,我正在尝试设置弹出错误。当我点击提交时,名称和电子邮件部分部分有效,但评论部分无效。我注意到,当我开始在评论部分输入内容时,它从大约 5 个空格开始,因此看起来该字段中有一些东西。一旦我删除它并提交表单,错误消息就会出现在评论部分,而不是像上面的 2 个字段那样位于字段的右侧?请帮忙?
<!DOCTYPE html>
<html>
<head>
<title>My Guestbook</title>
<style type="text/css">
.errors { color: ff0000; }
</style>
</head>
<body>
<?php /* Opening tag of php */
//initialize error array
$errors=array();
//main logic
if(isset($_REQUEST['submit'])){
checkForm();
}
else{
printForm();
}
//begin functions
function checkForm()
{
global $errors;
if($_POST[name] == "")
$errors['name']="<span class=\"errors\"><b>  Please, enter your Name!</span>";
if($_POST[email] == "")
$errors['email']="<span class=\"errors\"><b>  Please, enter your Email!</span>";
if($_POST[myComments] == "")
$errors['myComments']="<span class=\"errors\"><b>  Please, enter something! </span>";
if(count($errors) !=0)
printForm();
else
confirm();
} //end checkForm function
function confirm(){
print "<h2>Thank you for signing my guestbook</h2>";
print "<p>Name: ".$_POST['name'];
print "</p><p>Email: ".$_POST['email'];
print "</p><p>Comment: ".$_POST['myComments'];
print "</p><br />"; //Extra line break.
print "<em>Today is " . date('F jS, Y.')."</em><br />"; //Extra line break.
} //end confirm
function printForm()
{
global $errors;
$place = $_POST[place];
// My old code I am trying to combine
print <<< HERE
<h1>Please sign my guestbook.</h1>
<form method="POST" action="{$_SERVER['PHP_SELF']}">
<table>
<tr>
<td class=name </td>Name: <br />
<td><input type="text" name="name" id="name" value="{$_POST['name']}">
{$errors['name']}</td>
</tr>
<tr>
<td class=email</td>Email: <br />
<td><input type="text" name="email" id="email" value"{$_POST['email']}">
{$errors['email']}</td>
</tr>
<tr>
<td class=myComments</td>Comments:<br />
<td><textarea type="text" name="myComments" id="mycomments" rows="4" cols="40" value" {$_POST['myComments']}">
{$errors['myComments']}</textarea></td>
</tr>
<tr>
<td> </td>
<td><input type=submit name="submit" value="send"><input type="reset" name="clear" value=Clear> </td>
</tr>
</table>
</form>
<br>
HERE;
print "Today is " . date('F jS, Y.')."<br>"; //Date printed in page below form.
print "Form designed by Kevin O'Leary "."<br>"; //Date printed in page below form.
}
?>
</body>
</html>