如何提交联系表单而不是重定向到另一页

时间:2014-12-23 00:26:23

标签: php forms redirect contact

我无法找到任何提出类似问题的人 - 我构建了一个简单的联系表单,当我点击提交时,它会将我带到用于发送消息的php的页面。我想知道如何才能让这件事发生,而只是展示一个"谢谢你"提交按钮下面的消息,或类似的东西。

我的代码基于本教程。 http://www.freecontactform.com/html_form.php。表单发送的电子邮件很好,但我不希望它在提交后重定向到另一个页面。

    <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>

有用-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(); ?> 

我也知道我还需要做更多工作来确保表单更安全,但是现在我想要解决这个问题。有什么想法吗?谢谢你们!

2 个答案:

答案 0 :(得分:1)

使用Ajax:带jquery的示例帖子

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

或者您可以使用自己的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>';
 }?>

我个人喜欢做后者,但区别在于重装

Ps:表单中post方法的url应该是后者联系表单的同一页面。重定向将带您到同一个网站

答案 1 :(得分:0)

将代码放在联系表格中.. 并将行更改为   <form name="helpful-form" method="post" action="">

希望这有帮助,

相关问题