[英]How do I display message on the same page?
I have a quick question about my form. 我对我的表格有一个快速的问题。 After submitting the form you will be directed to a page followed by the text that I put in. The problem is that I want this text to be displayed on the same page as where the form is on (named contact.html) I use two files, one is the Mail_handler.php and the other contact.html. 提交表单后,您将被定向到页面,后跟我输入的文本。问题是我希望将此文本与表单所在的页面显示在同一页面上(名为contact.html),我使用两个文件,一个是Mail_handler.php,另一个是contact.html。 I've tried multiple things to fix it, but for some reason, I have no success. 我已经尝试了多种方法来修复它,但是由于某种原因,我没有成功。 I hope that you guys can help me out! 我希望你们能帮助我! Below you can find the HTML and PHP. 您可以在下面找到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!";
}
}
?> ?>
the best way is to use ajax, but if you may not do that, you can do this little trick: 最好的方法是使用ajax,但是如果您可能不这样做,则可以执行以下小技巧:
change contact.html into php script (maybe contact.php) 将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>
in the mail_handler.php, change echo to $msg 在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");
} ?>
Use AJAX 使用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'
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.