我正在使用bootstrap并且有一个我已经使用了一段时间的表单。它显示消息“Verzonden!”如果表单已成功提交,则在同一页面上。现在必须更改以在提交表单后重定向到新的succes页面。
这是我正在使用的HTML
<form role="form" id="contactForm" class="contact-form" data-toggle="validator" class="shake">
<div class="form-group">
<div class="controls">
<input type="text" id="name" class="form-control" placeholder="Naam & Voornaam" required data-error="Gelieve uw naam in te vullen.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="email" class="email form-control" id="email" placeholder="Email" required data-error="Gelieve uw email adres in te vullen.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" id="phone" class="form-control" placeholder="Telefoonnummer" required data-error="Gelieve uw telefoonnummer in te vullen.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" id="msg_subject" class="form-control" placeholder="Onderwerp" required data-error="Gelieve een onderwerp in te vullen.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<textarea id="message" rows="7" placeholder="Bericht" class="form-control" required data-error="Gelieve uw bericht in te vullen."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<button type="submit" id="submit" class="btn btn-success"></i>Verzend!</button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
</form>
这是我正在使用的php:
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Gelieve uw naam in te vullen.";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Gelieve uw email adres in te vullen.";
} else {
$email = $_POST["email"];
}
// PHONE
if (empty($_POST["phone"])) {
$errorMSG .= "Gelieve uw telefoonnummer in te vullen.";
} else {
$phone = $_POST["phone"];
}
// MSG SUBJECT
if (empty($_POST["msg_subject"])) {
$errorMSG .= "Gelieve een onderwerp in te vullen.";
} else {
$msg_subject = $_POST["msg_subject"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Gelieve een bericht in te vullen";
} else {
$message = $_POST["message"];
}
//Add your email here
$EmailTo = "e-mail address";
$Subject = "Nieuw bericht van website";
// prepare email body text
$Body = "";
$Body .= "Naam: ";
$Body .= "\n";
$Body .= $name;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= "\n";
$Body .= $email;
$Body .= "\n";
$Body .= "\n";
$Body .= "Telefoonnummer: ";
$Body .= "\n";
$Body .= $phone;
$Body .= "\n";
$Body .= "\n";
$Body .= "Onderwerp: ";
$Body .= "\n";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "\n";
$Body .= "Bericht: ";
$Body .= "\n";
$Body .= $message;
$Body .= "\n";
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "Verzonden!";
}else{
if($errorMSG == ""){
echo "Er is een fout opgetreden, probeert u aub opnieuw.";
} else {
echo $errorMSG;
}
}
?>
答案 0 :(得分:2)
您必须使用php标头功能将用户重定向到新页面:
// redirect to success page
if ($success && $errorMSG == ""){
header('Location: http://www.example.com/success-page.php');
}else{
if($errorMSG == ""){
echo "Er is een fout opgetreden, probeert u aub opnieuw.";
} else {
echo $errorMSG;
}
}
重要提示
永远不要在header()重定向调用之前打印或回显任何内容,否则它不会重定向用户。
阅读此处的参考资料: http://php.net/manual/en/function.header.php
<强>更新强>
// redirect to success page
if ($success && $errorMSG == ""){
header('Location: http://www.example.com/success-page.php');
}else{
if($errorMSG == ""){
header('Location: http://www.example.com/success-page.php');
} else {
echo $errorMSG;
}
}
还添加了if($errorMSG == "")
同时强>
最好在表单中添加操作属性,如下所示:
<form action="path-to-your-processing-script.php">
如果您没有提供它会在同一页面上提交表单,但仍然应该提供。