如何制作“提交”按钮刷新页面?

时间:2013-12-27 00:11:52

标签: php html echo contact-form

在我的网站上,我有一个“联系我们”表格。

目前,我正在使用php表单将表格上填写的信息通过电子邮件发送到我的电子邮箱。

我希望一旦有人点击提交就会显示一条回音消息,但是它会转到另一个包含该消息的页面。

我只想让页面刷新。

这是PHP代码:

     <?php


   //echo $emailBody;
   sendEmail();

 function sendEmail(){
 global $emailBody, $name, $from, $fromName, $templateFile, $to, $subject;

     $headers  = "MIME-Version: 1.0" . "\r\n";
     $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";    
     $headers .= "Reply-To: ". $from . "\r\n";    
     $headers .= "From: ". $from . "\r\n";
     $headers .= "Return-Path: ".$from;      

     if (!mail($to, $subject, $emailBody, $headers)) {

    } else {

    echo "Your Message has been sent!we will contact back you in a short moment!";
    }

}



 ?>

5 个答案:

答案 0 :(得分:0)

PHP将始终向您发送新页面,因为它是服务器端语言。为了在用户单击按钮然后向用户显示结果时执行此代码,必须将请求发送到服务器,执行,然后发送回用户。您可以像申请一样“刷新”页面的唯一方法是使用客户端语言,例如javascript。

就像这样:当用户点击提交按钮时,他们正在请求PHP处理请求。这只能在服务器上进行。因为请求从用户传递到服务器然后再返回给用户,所以总会得到一个新页面。

答案 1 :(得分:0)

需要更多代码,但通常在执行此类操作时,您可以将表单提交回iself并在php的顶部,执行一些isset(-form变量 - )检查,如果它们已填写然后你可以输入一个函数来从那里进行处理,然后将一条消息回显给用户,表明成功/失败。

答案 2 :(得分:0)

我会用:

<script type="text/javascript">
setTimeout(function(){location.reload();},1000);
</script>
<noscript>
<meta http-equiv="refresh" content="1" />
</noscript>

您也可以使用标头指定它(元刷新仅模拟此标头):

header('Refresh: 1');

给用户留下如下信息也很好:

<div>If this page doesn't automatically refresh in one second <a href="whatever">click here</a>.</div>

答案 3 :(得分:0)

更改您的action

<form action="This is where your webpage goes" method="POST">

所以这会将您的信息页发送到谷歌:

<form action="http://google.com" method="POST">

如果删除操作,它将重新加载当前页面,如下所示:

<form method="POST">

将您的php放在与<form>相同的页面中。

如下所示

<?php

if($_POST['submit'] == 'send'){ 

     sendEmail();

     function sendEmail(){
     global $emailBody, $name, $from, $fromName, $templateFile, $to, $subject;

         $headers  = "MIME-Version: 1.0" . "\r\n";
         $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";    
         $headers .= "Reply-To: ". $from . "\r\n";    
         $headers .= "From: ". $from . "\r\n";
         $headers .= "Return-Path: ".$from;      

        if (!mail($to, $subject, $emailBody, $headers)) {

        } else {
           echo "Your Message has been sent!we will contact back you in a short moment!";
        }

    }
}

//For that to work your button needs to be like this
?>
<form method="POST">
    <!-- other fields here -->
    <input type="submit" name="submit" value="send" />
</form>

答案 4 :(得分:0)

在回显之前,您可以使用要在其中显示的消息重定向到源页面

例如

if (!mail($to, $subject, $emailBody, $headers)) {

    } else {
  $myMsg="Your Message has been sent!we will contact back you in a short moment!";
  header('location: yourpage');
}

在您的页面中,您可以查看是否有任何此类消息

if(isset($myMsg))
{
 echo $myMsg; // in the place & style that you want 
}