我一直在尝试修复我的联系表格,其中数据可以通过电子邮件发送。但我一开始似乎有一些错误。它在网页中说“未定义的变量”。我只是在阅读我一直在阅读的教程,而且我还不擅长 PHP。我目前正在使用 XAMPP 来运行我的 PHP
这是 HTML 标记
<html>
<head>
<title>Contact Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Contact Form</h1>
<p class="error"> There are some misisng fields.</p>
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
<p class="sent">Thank you for sending your message</p><?php } ?>
<div class="contactform">
<form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="name">Name:</label>
<input type="text" name="name" />
<label for="email">Email:</label>
<input type="email" name="email" />
<label for="comments">Comments:</label>
<textarea name="comments"></textarea>
<input type="submit" name="submit" class="submit" value="submit" />
</form>
</div>
这是PHP代码
<?php if($_POST['submit']) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error = true;
} else {
$to = "clestcruz@gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent){
$sent= true;
}
}
}
?>
</body>
</html>
取消定义变量
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
if($_POST['submit']) {