使用PHP在html页面中显示消息状态 - 联系论坛

时间:2015-05-19 19:42:48

标签: php html contact-form

我的PHP设置到目前为止我的联系表格工作正常(我是PHP中的'新手')。

我正在使用echo来显示消息的状态(如果已发送或未发送)。用户单击发送按钮后,状态消息将显示在新URL中 - PHP文件的URL(somesite.com/forum.php)

我的问题是,我可以将我的状态消息显示在我的html文件中我的论坛所在的文件(Forum.html)基本上没有点击发送按钮,然后更改为forum.php的URL我可以有URL保持不变(Forum.html),但可能只是那里的状态文本(html文件)。

这是我的代码:

<?php

       $email = $_POST['email'];
       $name = $_POST['name'];
       $message = $_POST['message'];
       $from = 'From: Datcroft Website';
       $to = 'myemail@hotmail.co.uk';
       $subject = 'Datcroft Site Message';

       $body = "From: $name\n E-Mail: $email\n Message: $message\n";

       if ($_POST['submit']) {
           if (mail ($to, $subject, $body, $from)) {
           echo '<center><font color="lightgray"><p>Message Sent Successfully!</p> <p>I will get back in touch as soon as possible.</p></font></center>';
           } else {
           echo '<center><font color="lightgray"><p>Ah! Try again, please?</p></font></center>';
           }
       }
    ?>

我的代码的消息状态部分(我希望在forum.html中显示的内容不是新的网址:

 echo '<center><font color="lightgray"><p>Message Sent Successfully!</p> <p>I will get back in touch as soon as possible.</p></font></center>';
               } else {
               echo '<center><font color="lightgray"><p>Ah! Try again, please?</p></font></center>';

感谢任何能指出正确方向的人! 谢谢高级。

2 个答案:

答案 0 :(得分:2)

您应该将forum.html文件更改为forum.php,然后将您的PHP代码移至新forum.php文件的顶部,并将表单提交给自己:

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >

然后将你的if放在你要显示信息的位置。

答案 1 :(得分:0)

或者您可以使用flash消息(在会话中保存并且一次使用后会被删除的变量)从forum.php重定向并在html上显示它(如果已设置