如何在成功发送表单后显示我的谢谢文本?
现在它在提交后重新出现在页面的表单部分,但谢谢你的文字没有出现,它在我放入 - header('Location: index.php#contact');
之前有效,但现在它根本没有显示出来。我的代码如下:
PHP代码
<?php
if($_POST['submit']){
if(!$_POST['name']){
$error= "<br/>-Please enter your name" ;
}
if(!$_POST['email']){
$error.= "<br/>-Please enter your email" ;
}
if (trim($_POST['message']) == "")
{
$error.= "<br/>-Please enter message";
}
if(!$_POST['contact']!=$match){
$error.= "<br/>-Please enter your contact number" ;
}
if ($error){
$result= "Whoops, error: $error";
}
else{
mail('mahdi.mashrafi@yahoo.com', "Contact message", "Name: ".$_POST['name']." Email: ".$_POST['email']."
Email: ".$_POST['name']."
Message : ".$_POST['message']."
Contact :".$_POST['contact'] );
{
$result= "Thankyou, Ill be in touch shortly";
//to get the stored
session_start()
if(isset($_SESSION["result")){
$result=$_SESSION;
}
$_SESSION["result"]=$result;
header("location:index.php#contact?result=".$result);
$result=$_GET["result"];
}
}
}
?>
HTML&amp; PHP表格
<div id="contact" >
<div class="container">
<div class="row wowload fadeInLeftBig ">
<form method = "post" action = "" id = "contact-form " class="center" role ="form">
<!--Contact Starts-->
<div >
<div class="contactform center">
<h2 class="text-center wowload fadeInUp">Tell us about your <span>Project</span></h2><br>
<?php echo $result; ?>
<div class="col-sm-6 hello">
<input type = "text" name = "name" class = "form-control" placeholer = "Your name" value = "<?php echo $_POST['name'];?>" >
</div>
<div class="col-sm-6">
<input type = "email" name = "email" class = "form-control" placeholer = "Your email" value ="<?php echo $_POST['email'];?>">
</div>
<div class="col-sm-12">
<textarea name = "message" rows = "7" class = "form-control" placeholder = "message"><?php echo $_POST['message']; ?> </textarea>
</div>
<div class="col-sm-6">
<input type="text" name="contact" placeholder="Contact" value ="<?php echo $_POST['contact'];?>">
</div>
<div class="col-sm-6">
<input type="text" name="budget" placeholder="Project Budget"></div>`
</div>
<input type = "submit" name = "submit" class = "btn btn-secondary" value = "send message"/>
</div>
</form>
</div>
</div>
</div>
更新 PHP代码已更新,我不知道在答案中给出的代码放在哪里,因为我的PHP知识不是很好。
答案 0 :(得分:0)
会发生什么?
1.您设置变量$ result 2.你使用标题重定向
那是错的?
当你重定向一个新的PHP实例被打开。您的变量$ result不再设置。
如何解决?
1.在某处存储$ result(例如会话):
//to get the stored
session_start()
if(isset($_SESSION["result")){
$result=$_SESSION;
}
//to store
$_SESSION["result"]=$result;
2.将其放入网址:
header("location:http://url#contact?result=".$result);
//access it
$result=$_GET["result"];
答案 1 :(得分:0)
试试这个
if(mail(.......))
{
header('Location: index.php?result=Thankyou, Ill be in touch shortly');
exit();
}
的index.php
if(isset($_GET['result']))
{
echo $_GET['result'];
}