Contact form not working in wordpress

2019-03-06 04:56发布

问题:

I am trying to create a contact form in php. But the problem I am facing is that when ever I try to submit the form it shows 404 exception rather than the actual file which has the contact form. The theme is custom made. I have added my code below

<?php
if(isset($_POST)) {
    if(trim($_POST["name"]) === "") {
        $nameError = "Please enter your name.";
        $hasError = true;
    } else {
        $name = trim($_POST["name"]);
    }

    if(trim($_POST["email"]) === "")  {
        $emailError = "Please enter your email address.";
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST["email"]))) {
        $emailError = "You entered an invalid email address.";
        $hasError = true;
    } else {
        $email = trim($_POST["email"]);
    }
    if(trim($_POST["phone"]) === "")  {
        $phoneError = "Please enter your phone address.";
        $hasError = true;
    } else {
        $phone = trim($_POST["phone"]);
    }
    if(trim($_POST["comments"]) ==="") {
        $commentError = "Please enter a message.";
        $hasError = true;
    } else {
        if(function_exists("stripslashes")) {
            $comments = stripslashes(trim($_POST["comments"]));
        } else {
            $comments = trim($_POST["comments"]);
        }
    }

    if(!isset($hasError)) {
        $emailTo = get_option("tz_email");
        if (!isset($emailTo) || ($emailTo == "") ){
            $emailTo = get_option("admin_email");
        }
        $subject = "[PHP Snippets] From ".$name;
        $body = "Name: $name \n\nEmail: $email \n\nPhone:$phone\n\nComments: $comments";
        $headers = "From: ".$name." <".$emailTo.">" . "\r\n" . "Reply-To: " . $email;

        wp_mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
} 
?>
<?php get_header(); ?>

<div class="row" style="margin-top:150px;">
            <div class="bg-container"></div>
            <div class="col-md-4">
                <div class="card contact-form-card">
                    <form action="<?php esc_url(the_permalink());?>" method="post">
                        <fieldset>
                            <legend>Give us your details</legend>
                            <div class="form-group label-floating">
                              <label class="control-label" for="user-name">Enter your full name</label>
                            <input class="form-control" id="user-name" name="name" type="text">
                            </div>
                            <div class="form-group label-floating">
                              <label class="control-label" for="user-email">Enter your email id</label>
                            <input class="form-control" id="user-email" name="email" type="text">
                            </div>
                            <div class="form-group label-floating">
                              <label class="control-label" for="user-phone">Enter your phone number</label>
                            <input class="form-control" id="user-phone" name="phone" type="text">
                            </div>
                            <div class="form-group label-floating">
                              <label class="control-label" for="commenets">Comments</label>
                            <textarea class="form-control" id="commenets" name="comments" type="text"></textarea>
                            </div>
                            <button type="submit" name="submit" class="btn btn-primary btn-raised col-xs-12">Submit</button>
                        </fieldset>
                    </form>
                </div>
            </div>

            <div class="col-md-8">
                <h1 class="text-center">We are currently maintaining over 1000 properties in north Bangalore</h1>
            <div class="owl-carousel">
              <div class="item card">
                <p class="statement">"Poorna is an immaculate real estate consulting professional, and possess several unique skills up his sleeves like property consulting report writing that are rare to find in a professional framework."</p>
                        <p class="by">Shariq Saleem, Director, Armchair Solutions India</p>
              </div>
              <div class="item card">
                <p class="statement">"Square Capital Assets understands your needs clearly and they are very easy to work with. Once they taken over managing my property, it was a smooth ride"</p>
                        <p class="by">Manoj</p>
              </div>
            </div>
            </div>
            </div>
                <div class="row" style="margin-top:15px;">
                    <div class="col-sm-2">
                        <div class="col-xs-12 feature-tile text-center">
                            <img src=<?php echo get_template_directory_uri()."/img/sign_the_agreement.png";?> alt="Sign the agreement">
                            <h5>Sign with us</h5>
                        </div>
                    </div>
                    <div class="col-sm-2">
                        <div class="col-xs-12 feature-tile text-center">
                            <img src=<?php echo get_template_directory_uri()."/img/choose_your_tenants.png";?> alt="Choose your tenants">
                            <h5>We will find reliable tenants.</h5>
                        </div>
                    </div>
                    <div class="col-sm-2">
                        <div class="col-xs-12 feature-tile text-center">
                            <img src=<?php echo get_template_directory_uri()."/img/collect_security_deposit.png";?> alt="Collect Security Deposit">
                            <h5>Rental Agreement</h5>
                        </div>
                    </div>
                    <div class="col-sm-2">
                        <div class="col-xs-12 feature-tile text-center">
                            <img src=<?php echo get_template_directory_uri()."/img/rent_assured.png";?> alt="Rent Assured">
                            <h5>Collecting rent and keeping your home safe</h5>
                        </div>
                    </div>
                    <div class="col-sm-2">
                        <div class="col-xs-12 feature-tile text-center">
                            <img src=<?php echo get_template_directory_uri()."/img/repairs_under_2000.png";?> alt="Repairs Under Rs.2,000">
                            <h5>Regular check up and maintenance</h5>
                        </div>
                    </div>
                    <div class="col-sm-2">
                        <div class="col-xs-12 feature-tile text-center">
                            <img src=<?php echo get_template_directory_uri()."/img/property_tax.png";?> alt="Property Taxes?">
                            <h5>Managing taxes and bills</h5>
                        </div>
                    </div>
        </div>


<?php get_footer(); ?>

回答1:

Just change the fields name like below

 <input class="form-control" id="user-name" name="Fname" type="text">

 <input class="form-control" id="user-email" name="Femail" type="text">

You will not get 404 error

:)



回答2:

Naming a field in the form "name" caused that problem. I renamed it to customer name and it started working