mercredi 15 avril 2015

PHP wordpress contact form

I have been trying for hours but however I cant get the form to successfully send the email and redirect.At one point I was getting emails without it redirecting but that seems to of stopped. Its now just going to a 404 page without sending the email. Any help would be great. Thank you.



$errors = array();

$formName = sanitize($_POST['formName']);
$formEmail = sanitize($_POST['formEmail']);
$formSubject = sanitize($_POST['formSubject']);
$formMessage = sanitize($_POST['formMessage']);

$required_fields = array('formName', 'formEmail', 'formSubject', 'formMessage');
foreach($_POST as $key=>$value){
if(empty($value) && in_array($key, $_required_fields) === true){
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}

if(empty($errors)){
$to = 'pwnd999@msn.com';
$subject = $formSubject;
$body = $formMessage;
$header = 'From: ' . $formEmail;

wp_mail($to, $suject, $body, $header);

wp_redirect( get_permalink() . '?success' );
exit;

} else {
$errors[] = 'Something when wrong!';
}

function sanitize($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

}


if (isset($_GET[ 'success'])==true) { ?>
<div class="messageSent pannel">
<h1>Message sent successfully!</h1>
<p>I will try to reply as fast as possible however, feel free to send me a text message!</p>
</div>
<?php } else{ ?>

<form id="contactForm" method="POST" autocomplete="off" action="">
<?php if(!empty($errors)): ?>
<div class="errors pannel row">
<div class="col-md-8">
<ul>
<h3><li><?php echo implode('</li><li>', $errors);?></li><h3> </ul>
</div>
<div class="col-md-4">
<?php include(TEMPLATEPATH ."/panels/SVG/sadeFace.php"); ?>
</div>
</div>

<?php endif; ?>

<div class="form-group">
<label for="name"><h3>Name <span class="star">*</span></h3></label>
<span class="error"><?php echo $nameErr;?></span>
<div class="inputWrap">
<input type="text" required autocomplete="off" class="form-control" id="name" name="name" placeholder="Danny Devito" <?php echo isset($formName) ? 'value="' . $formName . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/name.php"); ?>
</div>
</div>
<div class="form-group">
<label for="email">
<h3>Email <span class="star">*</span></h3></label>
<div class="inputWrap">
<input type="email" required autocomplete="off" class="form-control" name="email" id="email" placeholder="devito@hollywood.com" <?php echo isset($formEmail) ? 'value="' . $formEmail . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/email.php"); ?>
</div>
</div>
<div class="form-group">
<label for="subject">
<h3>Subject <span class="star">*</span></h3></label>
<div class="inputWrap">
<input type="text" required autocomplete="off" class="form-control" name="subject" id="subject" placeholder="Design me a website!" <?php echo isset($formSubject) ? 'value="' . $formSubject . '"' : '' ?>>
<?php include(TEMPLATEPATH ."/panels/SVG/idea.php"); ?>
</div>
</div>
<div class="form-group">
<label for="message">
<h3>What can I help you with? <span class="star">*</span></h3></label>
<div class="inputWrap">
<textarea required autocomplete="off" name="message" class="form-control" id="message" placeholder="Hello Carl," value="poo">
<?php echo isset($formMessage) ? $formMessage : '' ?>
</textarea>
<?php include(TEMPLATEPATH ."/panels/SVG/message.php"); ?>
</div>
</div>
<button type="submit" name="submitted" class="btn main">Submit</button>
</form>
<?php }
?>

Aucun commentaire:

Enregistrer un commentaire