2017-03-08 10 views
1

私はブートストラップを使用しており、しばらく使用していたフォームを持っています。 「Verzonden!」というメッセージが表示されます。フォームが正常に提出された場合は同じページに表示されます。フォームが送信された後、新しいsuccesページにリダイレクトされるようにこれを変更する必要があります。フォーム提出後に別のsuccesページへのリダイレクト

これは私が

<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で使用していますhtmlです:

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

ヘッダーコールの前に何もエコーしていないと思います –

関連する問題