Carl Youngman
Carl Youngman

Reputation: 57

PHP wordpress contact form

im trying to send the email and redirect it, here is my code so far.

<?php 
if (isset($_POST['formName'], $_POST['formEmail'], $_POST['formSubject'], $_POST['formMessage'])) {

$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         = '[email protected]';
            $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="[email protected]" <?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 }
 ?>

Upvotes: 0

Views: 73

Answers (1)

d79
d79

Reputation: 3848

This is caused by the name of your field Name (name="name") that conflicts with the name parameter of WP_Query.

If you change that into name="formName" it should work.

Also you need to change the names of your other fields, not because of the previous reason, but because they don't match the names at the top of your code.

Upvotes: 2

Related Questions