简体   繁体   English

如何提交联系表而不重定向到其他页面

[英]How to Submit a Contact Form and Not Redirect to Another Page

I can't find anyone asking a similar question - I built a simply contact form and when I click submit, it takes me to the page on which the php for sending the message is written. 我找不到有人问类似的问题-我建立了一个简单的联系表单,当我单击“提交”时,它将带我到编写用于发送消息的php的页面。 I was wondering how I could make this not happen and instead just display a "thank you" message underneath the submit button, or something similar. 我想知道如何才能做到这一点,而只是在“提交”按钮或类似的内容下方显示“谢谢”消息。

I based my code off of this tutorial. 我的代码基于本教程。 http://www.freecontactform.com/html_form.php . http://www.freecontactform.com/html_form.php The form sends an email fine, but I just don't want it to redirect to another page after submission. 该表格可以很好地发送电子邮件,但是我不希望提交后将其重定向到另一页。

    <form name="helpful-form" method="post" action="../helpful-form.php"> 
        <input type="radio" name="unhelpful" value="The Page Did Not Answer My Questions">Did Not Answer My Questions</input><br>
        <input type="radio" name="unhelpful" value="The Page Content Was Not What I Was Expecting"><br>
        <input type="radio" name="unhelpful" value="There Was Too Much Information On The Page"><br>
        <input type="radio" name="unhelpful" value="There Was Too Little Information On The Page"<br>
        <input type="radio" name="unhelpful" value="The Page Content Was Too Complicated"><br>
        <textarea rows="3" cols="30" name="recommendations"></textarea>
        <input type="text" name="email">Email: (optional) </input>
        <input type="submit" value="submit" name="submit">
      </form>

helpful-form.php 乐于助人,form.php的

  <?php 

    $email_to = "*******************";
    $email_subject = "Website Recommendation/Question - Form Submission";

    function died($error) {
        echo ($error);
        die();
    }

    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }

    if (!isset($_POST['unhelpful']) && !isset($_POST['recommendations'])) {
        died('Please Select A Reason Why This Page Is Unhelpful.');
    }

        $unhelpful = $_POST['unhelpful'];
        $recommendations = $_POST['recommendations'];
        $email = $_POST['email'];

    $email_message = "A new website recommendation and/or question has been submitted from BestAttorney.com:\n";



    $email_message .= "This page was not helpful because: ".clean_string($unhelpful)."\n";
    $email_message .= "Other Comments from the User: ".clean_string($recommendations)."\n";
    $email_message .= "This Form Was Submitted by: ".clean_string($email)."\n";

    $headers = 'From: '.$email."\r\n".
    'Reply-To: '.$email."\r\n" . 
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers); 
?>

Thank you giving us your recommendations! We will respond to any questions you had in a timely manner. 

<?php die(); ?> 

I am also aware that there is more I have to do to make sure that the form is more secure but for now I'd like to just get this fixed up. 我也知道,我还有很多工作要做,以确保表格更安全,但现在我想修复这个问题。 Any thoughts? 有什么想法吗? Thanks guys! 多谢你们!

Use Ajax: Example post with jquery 使用Ajax:带有jquery的示例帖子

 $.post(url,data,function(data){alert('congratulations')});// I would go for showing a div that will fadeout with animation

or you can use you own php contact form with a special behavior, including this in your code 或者您可以使用具有特殊行为的自己的php联系人表单,包括此代码中的行为

<?php if (isset($_POST['email'])) //or whatever $_POST related condition and if so
{
  //saving code or mailing code etc

    echo '<div class="specialMessage">Thank you! </div>';
 }?>

I personally like to do the latter but the difference is the reload 我个人比较喜欢后者,但区别在于重新加载

Ps: the url of the post method in your form should be the same page of the contact form for the latter. 附言:您表单中post方法的网址应与后者的联系表单相同。 The redirect will take you to the same website 重定向会将您带到同一个网站

place the code in contact form.. and change the line to <form name="helpful-form" method="post" action=""> 将代码放在联系表单中。然后将行更改为<form name="helpful-form" method="post" action="">

Hope this helps, 希望这可以帮助,

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

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