【问题标题】:How to Submit a Contact Form and Not Redirect to Another Page如何提交联系表格而不重定向到另一个页面
【发布时间】:2014-12-23 00:26:22
【问题描述】:

我找不到任何人问类似的问题 - 我建立了一个简单的联系表单,当我单击提交时,它会将我带到编写用于发送消息的 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>

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

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

【问题讨论】:

  • 搜索 AJAX 教程。
  • 也许用关键字 AJAX 搜索?它会带你到这样的事情:blog.teamtreehouse.com/create-ajax-contact-form
  • 将所有内容放在同一个文件中并设置action="" 或使用Ajax,如前所述。
  • &lt;input type="radio" name="unhelpful" value="There Was Too Little Information On The Page"&lt;br&gt; 中还有一个语法错误,应该读作&lt;input type="radio" name="unhelpful" value="There Was Too Little Information On The Page"&gt;&lt;br&gt; 缺少&gt;
  • 感谢该教程链接!我不知道我需要 AJAX,但该教程看起来拥有我需要的一切。

标签: php forms redirect contact


【解决方案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 应该与后者的联系表单的页面相同。重定向会将您带到同一网站

【讨论】:

  • $_POST.length 不是有效的 PHP,它的 JS 如果有的话,你想要像 !empty($_POST) 这样的东西
【解决方案2】:

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

希望对你有帮助,

【讨论】:

  • 感谢您的回答 - 我这样做了,但它实际上只是重新加载了页面。我假设在不重新加载页面的情况下执行此操作的唯一方法是执行其他人所说的并使用 ajax。不过谢谢!
猜你喜欢
  • 2019-04-02
  • 1970-01-01
  • 2023-03-22
  • 2019-01-22
  • 2019-10-11
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多