联系表格重定向到PHP错误页面
我试图将我的HTML表单提交给PHP脚本(工作正常),但是PHP应该将它发送到电子邮件。相反,它最终会出现在错误页面上。所以我认为HTML代码不是问题,PHP中必须存在错误。任何人都可以帮助我解决这个错误?联系表格重定向到PHP错误页面
更新:现在有没有更多的错误,但它重定向我到一个空白网站..
更新HTML:
<form method="post" name="myform" action="form-to-email.php" id="myform">
<div class="form-group col-sm-5 col-sm-offset-1"> <!-- Date input -->
<label class="control-label" for="date1">Date of arrival</label>
<input class="form-control" id="date" name="date1" placeholder="MM/DD/YYY" type="text"/>
</div>
<div class="form-group col-sm-5 col-sm-offset-1"> <!-- Date input -->
<label class="control-label" for="date2">Date of departure</label>
<input class="form-control" id="date2" name="date2" placeholder="MM/DD/YYY" type="text"/>
</div>
<div class="form-group col-sm-5 col-sm-offset-1">
<label for="adults">Adults</label>
<select class="form-control" name="adults">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
</select>
</div>
<div class="form-group col-sm-5 col-sm-offset-1">
<label for="children">Children (under 14 years)</label>
<select class="form-control" name="children">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
</div><br><br>
<p class="location text-center col-xs-12"> Please enter your name and address:</p>
<div class="form-group col-sm-6 col-sm-offset-3">
<label for="title">Title</label>
<select class="form-control" name="title">
<option>Mr.</option>
<option>Mrs.</option>
</select>
</div>
<div class="form-group col-sm-6 col-sm-offset-3">
<label for="lastname">First name</label>
<input type="text" class="form-control" id="firstname" name="firstname">
</div>
<div class="form-group col-sm-6 col-sm-offset-3">
<label for="lastname">Last name</label>
<input type="text" class="form-control" id="lastname" name="lastname">
</div>
<div class="form-group col-sm-3 col-sm-offset-3">
<label for="lastname">Adress </label>
<input type="text" class="form-control" id="adress" name="adress">
</div>
<div class="form-group col-sm-3 col-sm-offset-1">
<label for="lastname">N°</label>
<input type="text" class="form-control" id="n°" name="n°">
</div>
<div class="form-group col-sm-3 col-sm-offset-3">
<label for="lastname">City </label>
<input type="text" class="form-control" id="city" name="city">
</div>
<div class="form-group col-sm-3 col-sm-offset-1">
<label for="zip">ZIP</label>
<input type="text" class="form-control" id="zip" name="zip">
</div>
<div class="form-group col-sm-6 col-sm-offset-3">
<label for="country">Country</label>
<input type="text" class="form-control" id="country" name="country">
</div>
<p class="location text-center col-xs-12"> Please enter your contact details:</p>
<div class="form-group col-sm-6 col-sm-offset-3">
<label for="email">E-mail</label>
<input type="text" class="form-control" id="email" name="email">
</div>
<div class="form-group col-sm-6 col-sm-offset-3">
<label for="mobile">Mobile number</label>
<input type="text" class="form-control" id="mobile" name="mobile">
</div> <div class="form-group col-sm-6 col-sm-offset-3">
<label for="comments">Comments</label>
<input type="text" class="form-control" id="comments" name="comments" style="height: 100px;">
</div>
<div class="form-group"> <!-- Submit button -->
<button class="btn btn-primary col-sm-offset-5" name="submit" id="submit" value="submit" type="submit">Submit</button>
</div>
</form>
更新PHP:
<?php
if(isset($_POST['submit'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Booking request";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['date1']) ||
!isset($_POST['date2']) ||
!isset($_POST['adults']) ||
!isset($_POST['children']) ||
!isset($_POST['title']) ||
!isset($_POST['firstname']) ||
!isset($_POST['lastname']) ||
!isset($_POST['adress']) ||
!isset($_POST['n°']) ||
!isset($_POST['city']) ||
!isset($_POST['zip']) ||
!isset($_POST['country']) ||
!isset($_POST['email']) ||
!isset($_POST['mobile'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$date1 = $_POST['date1']; // required
$date2 = $_POST['date2']; // required
$adults = $_POST['adults']; // required
$children = $_POST['children']; // required
$title = $_POST['title']; // required
$firstname = $_POST['firstname']; // required
$lastname = $_POST['lastname']; // required
$adress = $_POST['adress']; // required
$n° = $_POST['n°']; // required
$city = $_POST['city']; // required
$zip = $_POST['zip']; // required
$country = $_POST['country']; // required
$email = $_POST['email']; // required
$mobile = $_POST['mobile']; // not required
$comments = $_POST['comments']; // not required
$error_message = "Please fill all required fields.";
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$firstname)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
if(!preg_match($string_exp,$lastname)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Date of Arrival: ".clean_string($date1)."\n";
$email_message .= "Date of Departure: ".clean_string($date2)."\n";
$email_message .= "Adults: ".clean_string($adults)."\n";
$email_message .= "Children: ".clean_string($children)."\n";
$email_message .= "Title: ".clean_string($title)."\n";
$email_message .= "First Name: ".clean_string($firstname)."\n";
$email_message .= "Last Name: ".clean_string($lastname)."\n";
$email_message .= "Adress: ".clean_string($adress)."\n";
$email_message .= "N°: ".clean_string($n°)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "ZIP: ".clean_string($zip)."\n";
$email_message .= "Country: ".clean_string($country)."\n \n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Mobile: ".clean_string($mobile)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, $email_subject, $email_message, $headers);
header('Location: thank-you.html');
?>
<!-- include your own success html here -->
<?php
}
}
?>
的代码
if (isset($_POST["submit"]))
返回false
表单被回发时,因为虽然您有一个带有name="submit"
的按钮,但它没有value
属性。因此,当您提交表单时,“submit”变量为空,导致isset
返回false。
只需添加
value="submit"
到您的按钮,例如
<button class="btn btn-primary col-sm-offset-5" name="submit" id="submit" type="submit" value="submit">Submit</button>
N.B.没有要求value
有任何特定的内容。 value="x"
也会起作用 - 所有的代码测试都是存在一个值。
感谢您的回答,但它仍然无效,它卡在一个空白页面。我试图在我的本地服务器上提交表单,是不是? – Tioxx
或者我需要在本地主机上安装邮件服务器才能收到电子邮件? – Tioxx
只要你在localhost上使用它,并且PHP实际上已经安装好了,代码应该运行。它可能实际发送或不实际发送电子邮件,具体取决于服务器的配置方式,但您至少应该在屏幕上获得一些输出。它是否将您重定向到“thank-you.html”?或在此之前停止?如果你想追踪执行流程,你可以暂时在代码中放入一些'echo'或'var_dump'语句来查看它的起始位置(并且注释掉重定向头部,所以你不会被带到在它可以显示任何输出之前的另一个页面)。 – ADyson
代码中有一个语法错误'死了('我们很抱歉,但是您提交的表单看起来有问题');'应该是'死亡('我们很抱歉,但似乎对你提交的表单有问题');' –
@NileshDaladra如果你注意到,OP已经做了一个自定义函数,第8行。 – Rasclatt
如果您说的是php错误,请检查日志并发布您在日志文件中看到的内容。 – Andreas