我制作了一个完美的联系表格。我将 html 和 php 合并到一个 .php 文件中,但我注意到在我的 GoDaddy 服务器中使用 .htaccess 文件从我的网站中删除 PHP 扩展后,联系表单不再有效。如果单击提交按钮,它只会重新加载页面,没有错误消息,没有成功消息,什么都没有。
这是我使用的代码。
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['phoneNumber'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'xxxxxxxxxxx';
$subject = 'Client Enquiry';
$body =" From: $name\n E-Mail: $email\n Mobile: $number\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if number has been entered and is valid
if (!$_POST['phoneNumber']) {
$errNumber= 'Please enter your number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage) {
if (mail ($to, $subject, $body)) {
$result='<div class="alert alert-success">Thank You! We would get back to you within 24 hours.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html lang="en">
<body>
<section class="space">
<main class="site-main parallax">
</section>
<div id="conct" class="container-fluid" style="background-color: #333; padding-top: 25px; margin-top: 2px; ">
<div class="container col-md-6 col-md-offset-3" >
<h2 class="section-title" style=" text-align: center; color: white; font-size: 45px" ><span class="glyphicon glyphicon-send"></span> contact us</h2><br></br>
<form role="form" method="post" action="contact.php" >
<div class="form-group">
<label for="name"><span class="glyphicon glyphicon-user"></span> Full Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="full name"value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label for="email"><span class="glyphicon glyphicon-envelope"></span> Email:</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label for="phoneNumber"><span class="glyphicon glyphicon-earphone"></span> Mobile:</label>
<input type="text" class="form-control" id="phoneNumber" placeholder="telephone" name="phoneNumber" <?php echo htmlspecialchars($_POST['phoneNumber']); ?>>
<?php echo "<p class='text-danger'>$errNumber</p>";?>
</div>
<div class="form-group">
<label for="message"><span class="glyphicon glyphicon-comment"></span> Message:</label>
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="form-group text-center">
<input id="submit" name="submit" type="submit" value="Send" class="btn">
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-1" align="center" style=" padding-bottom: 10px">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</main>
</body>
</html>
我会继续添加我使用的重写规则。
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
如果有人能指出我正确的方向,我将不胜感激。