如何在同一页面上显示消息?

时间:2017-10-04 10:13:37

标签: php html forms email redirect

我对我的表单有一个简短的问题。提交表单后,您将被引导到一个页面,然后是我输入的文本。问题是我希望此文本显示在表单所在的同一页面上(名为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='infosyncdevelopment@gmail.com'; // 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!";
    }
}

&GT;

2 个答案:

答案 0 :(得分: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中

,将echo更改为$ msg

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

$to='infosyncdevelopment@gmail.com'; // 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");

} ?>

答案 1 :(得分: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'
        });
    });