简体   繁体   English

单击联系表单上的“提交”后,如何在wordpress中停留在同一页面上?

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

So I created a custom contact form in WordPress, using PHP. 因此,我使用PHP在WordPress中创建了一个自定义联系表单。 The form sends, and I am receiving emails. 表格发送,我正在接收电子邮件。 The problem I'm having is that once you hit submit, it goes to a post page, and doesn't stay on the original page. 我遇到的问题是,一旦您单击“提交”,它就会转到帖子页面,而不会停留在原始页面上。

I've tried using a session and header location (didn't work) I also tried putting this in my action" <?php echo $_SERVER['PHP_SELF']; ?> ", doesn't work either. 我试过使用会话和标头位置(不起作用),我也尝试将其置于动作中。“ <?php echo $_SERVER['PHP_SELF']; ?> ”,也不起作用。 (mail just doesn't send it and sends me to 404 page. (邮件只是不发送,而是将我发送到404页。

So I'm a little stuck, as to fix this problem. 因此,为了解决此问题,我有些困惑。 Normally I would have no problems if this was a static web page, but because I'm using WordPress, this task seems to be more troublesome. 通常,如果这是一个静态网页,我不会有任何问题,但是由于我使用的是WordPress,因此此任务似乎比较麻烦。

Here is a link to the website http://www.indianpointresort.ca/ 这是网站http://www.indianpointresort.ca/的链接

Here is the php validation: 这是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!
}
?>

Here is the form: 形式如下:

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

Try ajax form submission. 尝试ajax表单提交。 And add the insert query in a separate file. 并将insert query添加到一个单独的文件中。

Well, to start off I would remove that gmail account from your info (just to be safe). 好吧,首先,我会从您的信息中删除该gmail帐户(为了安全起见)。 Secondly I would advise you to use the sendmail scripts provided by Wordpress. 其次,我建议您使用Wordpress提供的sendmail脚本。 There are plugins like gravityforms which allow you to make a form and decide all these options without making a static form, nor a new template file for that matter. 有一些插件,例如gravityforms,可让您制作表格并决定所有这些选项,而无需制作静态表格,也无需使用新的模板文件。

You can only change to which page the form will redirect after the refresh (the action will decide that) 您只能更改刷新后表单将重定向到的页面(该操作将决定该页面)

If you want it to stay on the same page you can put the page itself in the action and on top put an if statement like 如果您希望它停留在同一页面上,则可以将页面本身放在动作中,并在其顶部放置if语句,例如

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

//validation, sendmail, and possibly errors here } else{ //show the form } //验证,sendmail,以及可能的错误,在这里} else {//显示表格}

anyway, a refreshing webform is as standard as it gets. 无论如何,令人耳目一新的网络表单已成为标准配置。 It's just how it submits things. 它就是提交事物的方式。 The only way you could prevent a page is by using jquery or javascript like so: (give your submit an id) 阻止网页的唯一方法是使用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