Php 表单提交后重定向到单独的成功页面
我正在使用bootstrap,有一个表单我已经使用了一段时间了。如果表单已成功提交,则在同一页上显示消息“Verzonden!”。现在,在提交表单后,必须将其更改为重定向到新的成功页面 这是我正在使用的htmlPhp 表单提交后重定向到单独的成功页面,php,html,forms,Php,Html,Forms,我正在使用bootstrap,有一个表单我已经使用了一段时间了。如果表单已成功提交,则在同一页上显示消息“Verzonden!”。现在,在提交表单后,必须将其更改为重定向到新的成功页面 这是我正在使用的html <form role="form" id="contactForm" class="contact-form" data-toggle="validator" class="shake"> <div class="form-group">
<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;
}
}
?>
您必须使用php header函数将用户重定向到新页面:
// 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;
}
}
重要提示
// 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;
}
}
在header()重定向调用之前,切勿打印或回显任何内容,否则它将不会重定向用户
请阅读此处的参考资料:
更新
// 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==“”)添加了条件
同时
最好在表单中添加action属性,如下所示:
<form action="path-to-your-processing-script.php">
如果您没有提供它将在同一页上提交表单,但您仍然应该提供它。我以前尝试过header函数,但当我这样做时,它不会提交表单并显示错误消息希望您在header callI更改代码之前没有回显任何内容,就像您发布的一样,但它显示了“Er是een fout opgetreden,probeert u aub opnieuw。”出错时应该执行的错误消息。请检查更新,它将正常工作。这意味着您将被重定向到新页面,但您的邮件尚未发送。尝试它仍然不会重定向它。