我正在管理一个营地的网站,该网站的表格可以让您在那里预订一个地方。然后使用PHP mail()方法在电子邮件中发送表单中的数据。我知道这可能不是最好/最安全的解决方案,但我对PHP不是很有经验,到目前为止它已经有效了。最近,该表单开始在提交时出现405错误。我的PHP有问题吗?
这是表单的html:
<div class="container" id="main">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid" id="navbar_container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#hlavni_navigace">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html" style="padding-top:8px">
<img class="logo" src="../img/ejpovice_drawing_250.png">
</a>
<a class="navbar-brand" href="index.html" style="letter-spacing:-0.1px;">
Autocamp
</a>
</div>
<div class="collapse navbar-collapse" id="hlavni_navigace" aria-expanded="false">
<ul class="nav navbar-nav">
<li><a href="index.html" >Home</a></li>
<li style="width:20%"><a href="accommodation.html">Accommodation</a></li>
<li><a href="gallery.html">Gallery</a></li>
<li class="active"><a href="reservations.html" class="active">Reservations<span class="sr-only">(current)</span></a></li>
</ul>
</div>
</div>
</nav>
<div class="objednavkovy_formular" style="margin-top:80px">
<div class="vnitrni_formular">
<form class="formular" method="POST" action='rezervace.php'>
<div class="form-group">
<label for="name">First name and surname: *</label><br>
<div class="input-group">
<input size="100" type="text" name="name" id="name" class="form-control" placeholder="Full name..." required>
<br>
</div>
</div>
<div class="form-group">
<label for="email">Email address: *</label><br>
<div class="input-group">
<input size="100" type="text" name="email" id="email" class="form-control" placeholder="example@gmail.com" value="@" required>
</div>
</div>
<div class="form-group">
<label for="telefon">Phone number: </label><br>
<div class="input-group">
<input type="text" name="telefon" id='telefon' class='form-control' placeholder='123456789'>
</div>
</div>
<div class="form-group">
<label for="od">From *</label>
<label for="do" style="margin-left:21%;">To: *</label><br>
<div class="input-group">
<script>
$(function() {
$( "#datepicker" ).datepicker({dateFormat: 'dd/mm/yy'});
});
$(function() {
$( "#datepicker2" ).datepicker({dateFormat: 'dd/mm/yy'});
});
</script>
<input type="text" name ="od" id="datepicker" size="100" style="height:0px;width:20%;" required>
<input type="text" name="do" id="datepicker2" size="100" style="height:0px;width:20%;margin-left:50px;" required>
</div>
</div>
<div class="form-group">
<label for="typ">Accommodation type: *</label><br>
<div class="input-group">
<select name="typ" style="color:#555;border:none;font-size:14px;padding:20px;" required>
<option value="nespecifikovano">--||--</option>
<option value="Karavan">Caravan</option>
<option value="Obyt_auto">Motorhome</option>
<option value="Stan">Tent</option>
<option value="Chata">Bungalow</option>
</select>
</div>
</div>
<div class="form-group">
<label for="pocet-lidi">Number of guests:</label><br>
<div class="input-group">
<input name="pocet-lidi" type="text" class="form-control" style="width:38%;color:#555;">
</div>
</div>
<div class="form-group">
<label for="poznamky">Notes:</label><br>
<div class="input-group" style="width:71%;">
<textarea cols="1" style="resize:none;padding-right: 12px;padding-left:12px;padding-top:6px;padding-bottom:6px;" rows="3" name="poznamky" id="poznamky" class="form-control" placeholder="Special requests - fridge ..."></textarea>
</div>
</div>
<input type="submit" name="submit" id="submit" value="Send" class="btn btn-primary" style="padding:15px;">
<?php
if ($success) {
echo $success;
}
?>
</form>
</div>
</div>
</div>
</div>
</body>
这是处理表单的PHP
<?php
if(isset($_POST['email'])) {
$email_to = "info@kemp.cz";
$email_subject = "Reservation";
function died($error) {
echo "We are sorry but your form contains errors and cannot be sent. ";
echo "The errors are the following: <br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix the errors in the form.<br /><br />";
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['poznamky']) ||
!isset($_POST['od']) ||
!isset($_POST['do'])) {
died('We are sorry but the form contains a mistake.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$poznamky = $_POST['poznamky']; // required
$od = $_POST['od'];
$do = $_POST['do'];
$typ = $_POST['typ'];
$cislo = $_POST['telefon'];
$host = $_POST['pocet-lidi'];
$from_email_address = "no-reply-rezervace@kemp.cz";
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= "Your email address isn't valid.<br />";
}
$string_exp = "/^[A-Za-zá-žÁ-Ž .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'Your name is not written in a valid way.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
if(strlen($host) > 2){
$error_message .= 'The number of guests is not valid. <br />';
}
$pocet_exp="/^[0-9]$/";
if (!preg_match($pocet_exp,$host)){
$error_message .= 'The number of guests is not valid. <br />';
}
$cislo_exp="/^[0-9]$/";
if (!preg_match($cislo_exp,$cislo)){
$error_message .= 'The phone number is not valid <br />';
}
$email_message = "Reservation confirmation:<br><br>";
$email_message2 = "Hi, <br> this email confirms your following reservation at Autocamp:<br><br>";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Full name: ".clean_string($name)."<br>";
$email_message .= "Email: ".clean_string($email_from)."<br>";
$email_message .= "Phone number: ".clean_string($cislo)."<br>";
$email_message .= "From: ".clean_string($od)."<br>";
$email_message .= "To: ".clean_string($do)."<br>";
$email_message .= "Accommodation type: ".clean_string($typ)."<br>";
$email_message .= "Number of guests: ".clean_string($host)."<br>";
$email_message .= "Notes: ".clean_string($poznamky)."<br>";
$email_message2 .= "Full name: ".clean_string($name)."<br>";
$email_message2 .= "Email: ".clean_string($email_from)."<br>";
$email_message2 .= "Phone number: ".clean_string($cislo)."<br>";
$email_message2 .= "From: ".clean_string($od)."<br>";
$email_message2 .= "To: ".clean_string($do)."<br>";
$email_message2 .= "Accommodation type: ".clean_string($typ)."<br>";
$email_message2 .= "Number of guests: ".clean_string($host)."<br>";
$email_message2 .= "Notes: ".clean_string($poznamky)."<br><br>";
$email_message2 .= "Thank you for your reservation and see you soon!";
$headers = 'From: '.$from_email_address."\r\n";
$headers .= 'Content-Type: text/html; charset="UTF-8"'."\r\n";
@mail($email_to, $email_subject, $email_message, $headers);
@mail($email_from, $email_subject, $email_message2, $headers);
header('Location: rezervace-success.html');
?>