简体   繁体   中英

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. 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. (mail just doesn't send it and sends me to 404 page.

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.

Here is a link to the website http://www.indianpointresort.ca/

Here is the php validation:

<?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. And add the insert query in a separate file.

Well, to start off I would remove that gmail account from your info (just to be safe). Secondly I would advise you to use the sendmail scripts provided by Wordpress. 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.

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(isset($_POST['submit'])){

//validation, sendmail, and possibly errors here } else{ //show the form }

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)

$('#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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM