【问题标题】:How do I display message on the same page?如何在同一页面上显示消息?
【发布时间】:2018-03-15 16:44:25
【问题描述】:

我有一个关于表单的快速问题。提交表单后,您将被定向到一个页面,然后是我输入的文本。问题是我希望此文本与表单所在的页面显示在同一页面上(名为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!";
    }
}

?>

【问题讨论】:

  • 您需要使用ajax调用并通过它提交。根据需要在表单上显示回复。

标签: php html forms email redirect


【解决方案1】:

最好的方法是使用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");

} ?>

【讨论】:

    【解决方案2】:

    使用 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'
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 2013-02-04
      • 2012-09-14
      相关资源
      最近更新 更多