这真的让我感动.....我一直得到一个未定义的变量,即使它在那里! 我该如何摆脱这个错误?更不用说由于使用xampp而不是在实时服务器上,我甚至不确定表单是否有效
contact.php上的表格
<div class="col-md-6 col-md-offset-3 emailForm">
<?php if (isset($result)) { echo "$result";} ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<label for="name">Your Name:</label>
<input type="text" name="name" class="form-control" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>" />
</div>
<div class="form-group">
<label for="subject">Subject:</label>
<input type="text" name="subject" class="form-control" value="<?php echo isset($_POST['name']) ? $_POST['subject'] : ''; ?>" />
</div>
<div class="form-group">
<label for="email">Your Email Address:</label>
<input type="email" name="email" class="form-control" value="<?php echo isset($_POST['name']) ? $_POST['email'] : ''; ?>" />
</div>
<div class="form-group">
<label for="message">Your Message:</label>
<textarea class="form-control" rows="5" name="message"><?php echo isset($_POST['name']) ? $_POST['message'] : ''; ?></textarea>
</div>
<div class="text-center">
<input type="submit" name="submit" class="btn btn-success btn-lg" value="Submit"/>
</div>
</form>
<!-- Contact Form Ends -->
</div>
sendemail.php代码
<?php
if(isset($_POST["submit"])) {
if (!$_POST['name']) {
$error="<br />Please enter your name";
}
if (!$_POST['subject']) {
$error.="<br />Please enter a subject";
}
if (!$_POST['email']) {
$error.="<br />Please enter your email address";
}
if (!$_POST['message']) {
$error.="<br />Please enter a message";
}
if ($_POST['email']!="" AND !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$error.="<br />Please enter a valid email address";
}
if ($error) {
$result='<div class="alert alert-danger"><strong>There were error(s) in your form:</strong>'.$error.'</div>';
} else {
if (mail("support@kf.com", "New Online Message!", "Name: ". $_POST['name']."
Name: ".$_POST['name']."
Subject: ".$_POST['subject'])) {
$result='<div class="alert alert-success text-center"><strong>Thank you!</strong> Please allow 24 to 48 hours for our support team to get back to you. Thank you. </div>';
} else {
$result='<div class="alert alert-danger">Sorry, there was an error sending your message. Please try again later.</div>';
}
}
}
?>
答案 0 :(得分:0)
尝试使用此代码:
<?php
$error = "";
if(isset($_POST["submit"])) {
if ($_POST['name'] == "") {
$error .="<br />Please enter your name";
}
if ($_POST['subject'] == "") {
$error.="<br />Please enter a subject";
}
if ($_POST['email'] == "") {
$error.="<br />Please enter your email address";
}
if ($_POST['message'] == "") {
$error.="<br />Please enter a message";
}
if ($_POST['email']!="" AND !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$error.="<br />Please enter a valid email address";
}
if ($error) {
$result='<div class="alert alert-danger"><strong>There were error(s) in your form:</strong>'.$error.'</div>';
} else {
if (mail("support@kanifinancial.com", "New Online Message!", "Name: ". $_POST['name']."
Name: ".$_POST['name']."
Subject: ".$_POST['subject'])) {
$result='<div class="alert alert-success text-center"><strong>Thank you!</strong> Please allow 24 to 48 hours for our support team to get back to you. Thank you. </div>';
} else {
$result='<div class="alert alert-danger">Sorry, there was an error sending your message. Please try again later.</div>';
}
}
}
?>
答案 1 :(得分:0)
你已经在if块中定义了$ error变量,它的范围仅在if块中。在开头的if块之外定义它。