2017-10-04 88 views
0

我對我的表單有個疑問。提交表單後,您將被導向到一個頁面,然後是我放入的文本。問題是我希望此文本與表單所在的頁面(名爲contact.html)顯示在同一頁面上我使用兩個文件,其中一個是Mail_handler.php,另一個是contact.html。我已經嘗試過多種方法來修復它,但出於某種原因,我沒有成功。我希望你們能幫助我!您可以在下面找到HTML和PHP。如何在同一頁面上顯示消息?

<form method="POST" action="mail_handler.php"> 
 
     <div class="col-sm-7 slideanim"> 
 
     <div class="row"> 
 
     <div class="col-sm-6 form-group"> 
 
      <input class="form-control" id="name" name="name" placeholder="Naam" type="text" required> 
 
      </div> 
 
      <div class="col-sm-6 form-group"> 
 
      <input class="form-control" id="phone" name="phone" placeholder="Telefoonnummer" type="text" required> 
 
     </div> 
 
      <div class="col-sm-12 form-group"> 
 
      <input class="form-control" id="email" name="email" placeholder="Email" type="email" required> 
 
     </div> 
 
     </div> 
 
     <textarea class="form-control" id="msg" name="msg" placeholder="Bericht" rows="5"></textarea><br> 
 
     <div class="row"> 
 
     <div class="col-sm-12 form-group"> 
 
     <button class="btn btn-default pull-right" id="submit" name="submit" type="submit">Verstuur</button> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 
     </div> 
 
    </form>

<?php 
if(isset($_POST['submit'])){ 
    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $phone=$_POST['phone']; 
    $msg=$_POST['msg']; 

    $to='[email protected]'; // Receiver Email ID, Replace with your email ID 
    $subject='Form Submission'; 
    $message="Name :".$name."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$msg; 
    $headers="From: ".$email; 

    if(mail($to, $subject, $message, $headers)){ 


     echo "<h1>Bedankt voor uw bericht!"." ".$name.", Wij nemen zo snel mogelijk contact met u op.</h1>"; 
    } 
    else{ 
     echo "Something went wrong!"; 
    } 
} 

>

+0

你需要使用'ajax'通話,並通過其提交的。只要您喜歡,就可以在表單上顯示回覆。 – Panther

回答

0

最好的辦法就是使用AJAX,但如果你可以不這樣做,你可以做這個小動作:

變化contact.html到php腳本(也許是contact.php)

<?php 
    if(isset($_GET['msg'])) echo $_GET['msg']; 
?> 
<form method="POST" action="mail_handler.php"> 
     <div class="col-sm-7 slideanim"> 
     <div class="row"> 
     <div class="col-sm-6 form-group"> 
      <input class="form-control" id="name" name="name" placeholder="Naam" type="text" required> 
      </div> 
      <div class="col-sm-6 form-group"> 
      <input class="form-control" id="phone" name="phone" placeholder="Telefoonnummer" type="text" required> 
     </div> 
      <div class="col-sm-12 form-group"> 
      <input class="form-control" id="email" name="email" placeholder="Email" type="email" required> 
     </div> 
     </div> 
     <textarea class="form-control" id="msg" name="msg" placeholder="Bericht" rows="5"></textarea><br> 
     <div class="row"> 
     <div class="col-sm-12 form-group"> 
     <button class="btn btn-default pull-right" id="submit" name="submit" type="submit">Verstuur</button> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 
     </div> 
    </form> 

在mail_handler.php,回聲改變爲$味精

<?php 
if(isset($_POST['submit'])){ 
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$msg=$_POST['msg']; 

$to='[email protected]'; // Receiver Email ID, Replace with your email ID 
$subject='Form Submission'; 
$message="Name :".$name."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$msg; 
$headers="From: ".$email; 

if(mail($to, $subject, $message, $headers)){ 


    $msg = "<h1>Bedankt voor uw bericht!"." ".$name.", Wij nemen zo snel mogelijk contact met u op.</h1>"; 
} 
else{ 
    $msg = "Something went wrong!"; 
} 

header("Location: contact.php?$msg"); 

} ?> 
+0

謝謝,它的工作原理! –

0

使用AJAX

$("form").submit(function(e) { 
    e.preventDefault(); 
    var name = $('#name').val(); 
    // do this for all other input tags 
     $.post("file.php" , {x:name , .....} , function(data){ 
       // do stuff with data , here data is the things echoed/printed by 'file.php' 
     }); 
    }); 
相關問題