我一直在嘗試解決我的聯繫表格,其中可以通過電子郵件發送數據。但我似乎在開始時有一些錯誤。它在網頁上顯示「Undefined variable」。我只跟隨一個我一直在閱讀的教程,而我還不熟練使用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 = "[email protected]";
$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']) {
個
'$ _POST'值是未識別的變量嗎? –
你好Jm請看我編輯的文章 – clestcruz