简体   繁体   English

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

[英]How do I make contact form success message appear in same page

My website is one pager with nav that links to different parts of the page within the same document. 我的网站是一个带有导航的寻呼机,链接到同一文档中的页面的不同部分。 So my contact is at stie.com/#contact rather than site.com/contact.html 所以我的联系方式是stie.com/#contact而不是site.com/contact.html

I have my contact form coded in html using post method linking to mail.php. 我的联系表单用html编码,使用post方法链接到mail.php。 Upon hitting the submit button I get redirected to site.com/mail.php where the "Your message was succesfully sent" is displayed. 点击提交按钮后,我会被重定向到site.com/mail.php,其中显示“您的消息已成功发送”。 How do I get it so that it displays right on top of the contact form since I don't have a contact.html file to turn into a contact.php and put the php code right where I want the success message to display? 如何将它显示在联系表单的顶部,因为我没有将contact.html文件转换为contact.php并将php代码放在我想要显示成功消息的位置?

<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 我的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>';
    }
?>

You can use URL parameters with PHP: 您可以在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>
    ...

Just set your form action URL to the same page with ?confirm=true at the end. 只需将表单操作URL设置为同一页面,最后使用?confirm=true

Make your action field empty. 将您的操作字段设为空。 Put action="" instead of action="mail.php" Then include your mail.php content inside your contact page. Put action=""而不是action="mail.php"然后在您的联系页面中包含您的mail.php内容。 As you know, you have to save that page as PHP, too; 如您所知,您还必须将该页面另存为PHP; for example, mycontactform.php. 例如,mycontactform.php。 In this way you have more control over the content and format of the "your message submitted" message. 通过这种方式,您可以更好地控制“您提交的消息”消息的内容和格式。 If you separate mail.php you can't address divisions in the mycontactform.php. 如果你将mail.php分开,则无法解决mycontactform.php中的分歧问题。

Security and vulnerability of PHP codes you are using should be addressed after you have completed the page coding and tested it as up and running in your desired format, since it needs more in-depth study of PHP conventions and usages. 在您完成页面编码并以所需格式运行并运行后,应该解决您正在使用的PHP代码的安全性和漏洞,因为它需要对PHP约定和用法进行更深入的研究。 source: A Set of Step by Step Tutorials Using HTML5, CSS3 and PHP (8) source: 一套使用HTML5,CSS3和PHP的循序渐进教程(8)

Note that your script mail.php is vulnerable to headers injection attack. 请注意,您的脚本mail.php容易受到标头注入攻击。 You need to escape your variable $_POST['email']. 你需要转义变量$ _POST ['email']。 You have to remove the special characters \\n and \\r . 您必须删除特殊字符\\ n\\ r \\ n This can be made easily by using the str_replace function. 使用str_replace函数可以轻松完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM