0

I have a php contact form on a couple of static sites, and I've cut and pasted the form itself and the php code into a WordPress site.

For some reason the form isn't working? Is there something extra I need to do with forms on a WordPress site (it's a custom theme and the code is on a contact page). The contact form information is (theoretically) being sent to an email address when submitted.

When I fill in and submit the form it just throws a 404 page error. The php is placed at the top of the page below the get_header(); function.

Any tips or pointers would be hugely appreciated.

PHP

<?php 

if($_POST['submit']) {

    if(!$_POST['name']) {
        $error="<br>- Please enter your name";
    }
    if(!$_POST['email']) {
        $error.="<br>- Please enter your email";
    }
    if(!$_POST['telephone']) {
        $error.="<br>- Please enter your telephone number";
    }
    if(!$_POST['message']) {
        $error.="<br>- Please enter your message";
    }
    if(!$_POST['checkbox']) {
        $error.="<br>- Please confirm you agree to the Privacy Policy";
    }

    if ($error) {
        $result='<div class="alert error">Whoops, there is an error. Please correct the following: '.$error.'</div>';
    } else {
        mail("myemail@example.com", "Contact Message", "Name: ".htmlspecialchars($_POST['name'])."
        Email: ".htmlspecialchars($_POST['email'])."
        Telephone: ".htmlspecialchars($_POST['telephone'])."
        Company: ".htmlspecialchars($_POST['company'])."
        Budget: ".htmlspecialchars($_POST['budget'])."
        Message: ".htmlspecialchars($_POST['message']),
        "From: Website_Inquiry@inset.agency\r\n"
    );

        {
            $_POST= array();
            $result='<div class="alert thankyou" role="alert">THANK YOU! WE\'LL BE IN TOUCH SHORTLY...</div>';
        }

        $headers = "From: webmaster@example.com\r\n";
    }
}
?>

HTML

<form id="contactform" method="post" action="#section-2">
    <?php echo $result; ?>
    <div class="form-row-1 js-st">
        <input id="name" type="text" name="name" placeholder="Name" value="<?php echo $_POST['name'];?>">
        <input id="email" type="email" name="email" placeholder="Email Address" value="<?php echo $_POST['email'];?>">
    </div>
    <div class="form-row-2 js-st">
        <input id="telno" type="text" name="telephone" placeholder="Telephone Number" value="<?php echo $_POST['telephone'];?>">
        <input id="company" type="text" name="company" placeholder="Company [optional]" value="<?php echo $_POST['company'];?>">
    </div>
    <div class="form-row-3 js-st">
        <textarea id="project-details" name="message" placeholder="Tell us about your project"><?php echo $_POST['message'];?></textarea>
    </div>
    <div class="form-row-4 js-st">
        <input id="budget" type="text" name="budget" placeholder="Budget" value="<?php echo $_POST['budget'];?>">
        <p class="budget-subline tl ">*We recommend putting in an approximate budget</p>
        <input id="privacy-checkbox" type="checkbox" name="checkbox"><label id="privacy-label" for="privacy-checkbox">I agree to the <a href="./privacy.php">privacy policy</a></label>
    </div>
    <input class="js-st" id="formsubmit" type="submit" name="submit" value="SUBMIT">
</form>
1

WordPress uses Name, email post fields to do post comments, If you renamed the your form elements name attributes to be f-name and f-email then your issue will be resolved. I have made changes in below code and it is working now.

<?php 
if($_POST['f-submit']) {

    if(!$_POST['f-name']) {
        $error="<br>- Please enter your name";
    }
    if(!$_POST['f-email']) {
        $error.="<br>- Please enter your email";
    }
    if(!$_POST['f-telephone']) {
        $error.="<br>- Please enter your telephone number";
    }
    if(!$_POST['f-message']) {
        $error.="<br>- Please enter your message";
    }
    if(!$_POST['f-checkbox']) {
        $error.="<br>- Please confirm you agree to the Privacy Policy";
    }

    if ($error) {
        $result='<div class="alert error">Whoops, there is an error. Please correct the following: '.$error.'</div>';
    } else {
        mail("myemail@example.com", "Contact Message", "Name: ".htmlspecialchars($_POST['f-name'])."
        Email: ".htmlspecialchars($_POST['f-email'])."
        Telephone: ".htmlspecialchars($_POST['f-telephone'])."
        Company: ".htmlspecialchars($_POST['f-company'])."
        Budget: ".htmlspecialchars($_POST['f-budget'])."
        Message: ".htmlspecialchars($_POST['f-message']),
        "From: Website_Inquiry@inset.agency\r\n"
    );

        {
            $_POST= array();
            $result='<div class="alert thankyou" role="alert">THANK YOU! WE\'LL BE IN TOUCH SHORTLY...</div>';
        }

        $headers = "From: webmaster@example.com\r\n";

    }
}
?>

HTML

<form id="contactform" method="post" action="#section-2">
        <?php echo $result; ?>
        <div class="form-row-1 js-st">
            <input id="name" type="text" name="f-name" placeholder="Name" value="<?php echo $_POST['f-name'];?>">
            <input id="email" type="email" name="f-email" placeholder="Email Address" value="<?php echo $_POST['f-email'];?>">
        </div>
        <div class="form-row-2 js-st">
            <input id="telno" type="text" name="f-telephone" placeholder="Telephone Number" value="<?php echo $_POST['f-telephone'];?>">
            <input id="company" type="text" name="f-company" placeholder="Company [optional]" value="<?php echo $_POST['f-company'];?>">
        </div>
        <div class="form-row-3 js-st">
            <textarea id="project-details" name="f-message" placeholder="Tell us about your project"><?php echo $_POST['f-message'];?></textarea>
        </div>
        <div class="form-row-4 js-st">
            <input id="budget" type="text" name="f-budget" placeholder="Budget" value="<?php echo $_POST['f-budget'];?>">
            <p class="budget-subline tl ">*We recommend putting in an approximate budget</p>
            <input id="privacy-checkbox" type="checkbox" name="f-checkbox"><label id="privacy-label" for="privacy-checkbox">I agree to the <a href="./privacy.php">privacy policy</a></label>
        </div>
        <input class="js-st" id="formsubmit" type="submit" name="f-submit" value="SUBMIT">
    </form>
| improve this answer | |
  • 1
    Thanks Chetan that works a treat now ! – Paul12_ Mar 13 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.