2017-03-08 226 views
1

我正在使用bootstrap,并有一个我现在已经使用了一段时间的表单。它显示消息“Verzonden!”如果表单已成功提交,则在同一页上。现在必须在提交表单后将其更改为重定向到新的连续页面。重定向到表单提交后单独的连续页面

这是我使用

<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> 

这个HTML是我使用的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; 
    } 
} 

?> 

回答

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

UPDATE

// 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"> 

如果你不提供,这将提交表单在同一页上,但仍然您都应该给它。

+0

我试过了标题功能,但是当我这样做时,它不会提交表单并显示错误信息 – Daybreaker

+0

请您分享错误信息吗? –

+0

希望你在标题调用前没有回应任何内容 –