如何使联系表单成功消息显示在同一页面中

时间:2013-11-12 21:37:24

标签: php html html5 forms email

我的网站是一个带有导航的寻呼机,可链接到同一文档中页面的不同部分。所以我的联系方式是stie.com/#contact而不是site.com/contact.html

我的联系表单用html编码,使用post方法链接到mail.php。点击提交按钮后,我会被重定向到site.com/mail.php,其中显示“您的消息已成功发送”。如何将它显示在联系表单的顶部,因为我没有将contact.html文件转换为contact.php并将php代码放在我想要显示成功消息的位置? / p>

<div class="row">
<div class="12u">
<form method="post" action="mail.php">
<div>
<div class="row half">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="6u">
<input type="email" name="email" id="email" placeholder="Email" />
</div>
</div>
<div class="row half">
<div class="12u">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<a href="#" class="button form-button-submit">Send Message</a>
<a href="#" class="button button-alt form-button-reset">Clear Form</a>
</div>
</div>
</div>
</form>
</div>

My Mail.php

<?php

//GET INFO FROM CONTACT FORM
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST ['subject'];
$message = $_POST['message'];
$from .= $_POST ['email'];
$to = 'email@site.com';

// compose headers
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";

//POST SUBMIT
if ($_POST['sumbit']);
    if ($name != '' && $subject != '' && $message !='' && $email != '') {                
            if (mail ($to, $subject, $from, $message, $headers)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    } else {
        echo '<p>Please fill in all required fields!!</p>';
    }
?>

3 个答案:

答案 0 :(得分:1)

您可以在PHP中使用URL参数:

<?php

$confDisplay = 'display:none;';

// if the url param exists, display confirmation
if(isset($_GET["confirm"]) && $_GET["confirm"]==true){
  $confDisplay = 'display:inline;';
}

?>

    ...
    <div style="<?php echo $confDisplay; ?>">
    Your form has been submitted!
    </div>
    ...

只需将表单操作网址设置为最后一位?confirm=true的同一页面。

答案 1 :(得分:1)

将您的操作字段设为空。将action=""代替action="mail.php"然后在您的联系页面中包含您的mail.php内容。如您所知,您还必须将该页面另存为PHP;例如,mycontactform.php。通过这种方式,您可以更好地控制“您提交的消息”消息的内容和格式。如果你将mail.php分开,则无法解决mycontactform.php中的分歧问题。

在完成页面编码并以所需格式运行并运行后,应解决您正在使用的PHP代码的安全性和漏洞,因为它需要对PHP约定和用法进行更深入的研究。来源:A Set of Step by Step Tutorials Using HTML5, CSS3 and PHP (8)

答案 2 :(得分:0)

请注意,您的脚本 mail.php 容易受到标头注入攻击。你需要转义变量$ _POST ['email']。您必须删除特殊字符 \ n \ r 。使用str_replace函数可以轻松完成此操作。