在联系表单上单击提交后,如何在wordpress中保持同一页面?

时间:2014-04-05 17:22:44

标签: php jquery html wordpress forms

所以我使用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-->

2 个答案:

答案 0 :(得分:0)

尝试ajax表单提交。并将insert query添加到单独的文件中。

答案 1 :(得分:0)

好吧,首先,我会从您的信息中删除该gmail帐户(只是为了安全起见)。 其次,我建议你使用Wordpress提供的sendmail脚本。 有一些插件,比如gravityforms,它允许你创建一个表单并决定所有这些选项而不需要制作静态表单,也不需要新的模板文件。

您只能更改表单在刷新后重定向的页面(操作将决定)

如果您希望它保持在同一页面上,您可以将页面本身放在操作中,并在顶部放置一个if语句,如

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

//验证,sendmail,以及可能的错误 } 其他{ //显示表单 }

无论如何,一个令人耳目一新的网络表格是标准的。这就是它提交的方式。阻止页面的唯一方法是使用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();

}
相关问题