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("[email protected]", "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: [email protected]\r\n"
);
{
$_POST= array();
$result='<div class="alert thankyou" role="alert">THANK YOU! WE\'LL BE IN TOUCH SHORTLY...</div>';
}
$headers = "From: [email protected]\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>