簡體   English   中英

單擊聯系表單上的“提交”后,如何在wordpress中停留在同一頁面上?

[英]after clicking submit on a contact form, how do i stay on the same page in wordpress?

因此,我使用PHP在WordPress中創建了一個自定義聯系表單。 表格發送,我正在接收電子郵件。 我遇到的問題是,一旦您單擊“提交”,它就會轉到帖子頁面,而不會停留在原始頁面上。

我試過使用會話和標頭位置(不起作用),我也嘗試將其置於動作中。“ <?php echo $_SERVER['PHP_SELF']; ?> ”,也不起作用。 (郵件只是不發送,而是將我發送到404頁。

因此,為了解決此問題,我有些困惑。 通常,如果這是一個靜態網頁,我不會有任何問題,但是由於我使用的是WordPress,因此此任務似乎比較麻煩。

這是網站http://www.indianpointresort.ca/的鏈接

這是php驗證:

<?php

    /*session_start();
    if(!isset($_SESSION['afaisfjisjfijfjiwaefjawsefijef'])){
        $url = 'http://www.indianpointresort.ca/';
        header("Location:home.php?url=$url");
    }*/

    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);

    echo "$name | $email | $phone | $subject | $message";   


    if(isset($_POST['submit'])){

        $boolValidationOK = 1;
        $strValidationMessage = "";

        //validate first name
        //validate last name
        if(strlen($name)<3){
            $boolValidationOK = 0;
            $strValidationMessage .= "Please fill in a proper first and last name </br>";
        }
        //email validation:
        $emailValidate =  validate_email( $email );// calls the function below to validate the email addy
        if(!$emailValidate ){
             $boolValidationOK = 0;
            $strValidationMessage .= "Please fill in proper email address </br>";
        }
        //validate phone
        $phone = checkPhoneNumber($phone);
        if(!$phone){
            $boolValidationOK = 0;
            $strValidationMessage .= "Please fill proper phone number </br>";
        }
        //validate subject
        if(strlen($subject)<3){
            $boolValidationOK = 0;
            $strValidationMessage .= "Please fill in a proper subject description </br>";
        }
        //validate description
        if(strlen($message)<3){
            $boolValidationOK = 0;
            $strValidationMessage .= "Please fill in a proper message </br>";
        }
        if($boolValidationOK == 1){
            //$strValidationMessage = "SUCCESS";

            //MAIL SECURITY !!!!!!!


    // WE MUST VALIDATE AGAINST EMAIL INJECTIONS; THE SPAMMERS BEST WEAPON
    $badStrings = array("Content-Type:",
    "MIME-Version:",
    "Content-Transfer-Encoding:",
    "bcc:",
    "cc:");
    foreach($_POST as $k => $v){// change to $_POST if your form was method="post"
        foreach($badStrings as $v2){
            if(strpos($v, $v2) !== false){
                // In case of spam, all actions taken here
                //header("HTTP/1.0 403 Forbidden");
                echo "<script>document.location =\"http://www.bermuda-triangle.org/\" </script>";
                exit; // stop all further PHP scripting, so mail will not be sent.
            }
        }
    }


    $ip = $_SERVER['REMOTE_ADDR'];
    //echo $ip;


    /* Spammer List: IP's that have spammed you before ***********/
            $spams = array (
             "static.16.86.46.78.clients.your-server.de", 
             "87.101.244.8", 
             "144.229.34.5", 
             "89.248.168.70",
             "reserve.cableplus.com.cn",
             "94.102.60.182",
             "194.8.75.145",
             "194.8.75.50",
             "194.8.75.62",
             "194.170.32.252"
             //"S0106004005289027.ed.shawcable.net"  Phil's IP as test 

        ); // array of evil spammers

        foreach ($spams as $site) {// Redirect known spammers
            $pattern = "/$site/i";
            if (preg_match ($pattern, $ip)) {
                // whatever you want to do for the spammer
                echo "logging spam activity..";

                exit();
            }
        }   
        $to = "";
        //$subject = " Indian Point";
        // compose headers
        $headers = "From: Indian Point Resort.\r\n";
        $headers .= "Reply-To: $email\r\n";
        $headers .= "X-Mailer: PHP/".phpversion();

        $message = wordwrap($message, 70);

        // send email
        mail($to, $subject, $message, $headers);
            }
    }//end of submit

    //validate phone number
    function checkPhoneNumber($number){
        $number = str_replace("-", "", $number);
        $number = str_replace(".", "", $number);
        $number = str_replace(" ", "", $number);
        $number = str_replace(",", "", $number);
        $number = str_replace("(", "", $number);
        $number = str_replace(")", "", $number);

        if((strlen($number) != 10) || (!is_numeric($number))){
            return false;
        }else{
            return $number;
        }
    }
    //email validation
    function validate_email( $senderemail ){ // this is a function; it receives info and returns a value.
    $email = trim( $senderemail ); # removes whitespace
     if(!empty($email) ):
        //  validate email address syntax
       if( preg_match('/^[a-z0-9\_\.]+@[a-z0-9\-]+\.[a-z]+\.?[a-z]{1,4}$/i', $email, $match) ):
         return strtolower($match[0]); # valid!
       endif;
     endif;
     return false; # NOT valid!
}
?>

形式如下:

   <div id="msgForm" class=" msgForm five columns">
                                    <h4>Questions?</h4>
                                    <h5>Send us a message!</h5>
                                    <form id="contactForm" name="contactForm" method="post" action="<?php the_permalink(); ?>">
                                        <p><input type="text" name="name" value="<?php echo $name; ?>" placeholder="name*"/></p>
                                        <p><input type="email" name="email" placeholder="E-mail*"/></p>
                                        <p><input type="text" name="phone" placeholder="Phone #*"/></p>
                                        <p><input type="text" name="subject" placeholder="subject*"/></p>
                                        <p><textarea name="message" placeholder="Message*"></textarea></p>
                                        <p><input type="submit" name="submit" placeholder="Submit"/></p>
                                        <div class="error">
                                        <?php
                                        if($strValidationMessage){
                                            echo $strValidationMessage;
                                        }
                                        ?>
                                        </div>  
                                    </form>
                                </div><!--end of form-->

嘗試ajax表單提交。 並將insert query添加到一個單獨的文件中。

好吧,首先,我會從您的信息中刪除該gmail帳戶(為了安全起見)。 其次,我建議您使用Wordpress提供的sendmail腳本。 有一些插件,例如gravityforms,可讓您制作表格並決定所有這些選項,而無需制作靜態表格,也無需使用新的模板文件。

您只能更改刷新后表單將重定向到的頁面(該操作將決定該頁面)

如果您希望它停留在同一頁面上,則可以將頁面本身放在動作中,並在其頂部放置if語句,例如

if(isset($_POST['submit'])){

//驗證,sendmail,以及可能的錯誤,在這里} else {//顯示表格}

無論如何,令人耳目一新的網絡表單已成為標准配置。 它就是提交事物的方式。 阻止網頁的唯一方法是使用jquery或javascript,如下所示:(給您提交ID)

$('#submit').on("click", function(e){
//this prevents any submit functionality (like refresh)
e.preventDefault();
//custom code to get values here and put them in the sendmail function like so:
var message = $('$message').text();

}

暫無
暫無

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

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