简体   繁体   中英

Form isn't sending

I'm staging a site for a client and I'm trying to create a form from scratch rather than use a plugin.

I'm not sure where I'm going wrong. The page keeps refreshing to the homepage and no email gets sent.

Could someone please point out in my code where I've gone wrong...

Thanks in advance!

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'From: Test'; 
        $to = 'email@example.com'; 
        $subject = 'Hello';

        if ($name == "" OR $email == "") {
            echo "You must specify a value for name, email address, and message.";
            exit;
        }

        foreach( $_POST as $value ){
                if( stripos($value,'Content-Type:') !== FALSE ){
                    echo "There was a problem with the information you entered.";
                    exit;
                }
        }

        require_once("assets/inc/phpmailer/class.phpmailer.php");
        $mail = new PHPMailer();

        if (!$mail->ValidateAddress($email)){
            echo "You must specify a valid email address.";
        }

        $body = "From: $name\n E-Mail: $email\n Message:\n $message";

        header("Location: http://natashamcdiarmid.com/clients/JLP/wp/contact/?status=thanks");
        exit;
    }
?>




<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
    <p>Thanks, I'll get back to your shortly!</p>
<?php } else ?>

<form method="post" action="contact">

    <p><label>Name</label></p>
    <input name="name" placeholder="Type Here">

    <p><label>Email</label></p>
    <input name="email" type="email" placeholder="Type Here">

    <p><label>Message</label></p>
    <textarea name="message" placeholder="Type Here"></textarea>

    <p><label>*What is 2+2?</label></p>
    <input name="human" placeholder="Type Here">

    <p><input id="submit" name="submit" type="submit" value="Submit"></p>

    <?php
        if ($_POST['submit']) {
            if (mail ($to, $subject, $body, $from)) { 
                echo '<p>Your message has been sent!</p>';
            } else { 
                echo '<p>Something went wrong, go back and try again!</p>'; 
            }
        }
    ?>

</form>    

The form that you are posting to isn't much of a directory.

<form method="post" action="contact">  
 should it be contact.php?

this action should be the directory of your form handler

A major issue with WordPress that always gets me is that it uses some request variables with common names, and messing with them causes unpredictable errors. For instance, the name parameter is used to locate and display the current post or web page.

Try renaming your name field to something else, like your_name .

When I create forms for use in WordPress, typically I prefix every field with something custom, like acme_contact_name , acme_contact_email , etc. A little more typing, but safer.

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