簡體   English   中英

如何提交聯系表而不重定向到其他頁面

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

我找不到有人問類似的問題-我建立了一個簡單的聯系表單,當我單擊“提交”時,它將帶我到編寫用於發送消息的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(); ?> 

我也知道,我還有很多工作要做,以確保表格更安全,但現在我想修復這個問題。 有什么想法嗎? 多謝你們!

使用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>';
 }?>

我個人比較喜歡后者,但區別在於重新加載

附言:您表單中post方法的網址應與后者的聯系表單相同。 重定向會將您帶到同一個網站

將代碼放在聯系表單中。然后將行更改為<form name="helpful-form" method="post" action="">

希望這可以幫助,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM