ttmt
ttmt

Reputation: 4984

Wordpress Email Form

Wordpress Email Form

Hi all

I've been using this sort of email form in a number of sites and it's always worked.

I've tried to use it in a Wordpress site but it won't

Is there any obvious reason why this approach won't work in a Wordpress site.

I need an email form that doesn't reload the contact page as the form is at the bottom.

html

    <form action="#" id="contact_form">
         <input type="text" id="name" placeholder="Name:">
         <input type="text" id="email" placeholder="Email:">
         <textarea id="message" rows="8" cols="40" placeholder="Message:"></textarea>
                <input type="submit" id="submit"/>
                <div id="status">
           <p></p>
         </div>
     </form>

Jquery

      $('#submit').click(function(){    
        //
        var nameVal = $('#contact_form #name').val();
        var emailVal = $('#contact_form #email').val();
        var messageVal = $('#contact_form #message').val();
        //
        $.post('/contact_form.php', {name: nameVal, email: emailVal, message: messageVal}, function(data){
            $("#status p").html(data);
            $("#status p").show().fadeOut(3500);
            if(data.indexOf('Thank You')==0) {document.forms[0].reset();}
        });
      })

php

        $errors = array();
        $required_fields = array('name','email','message');
        foreach($required_fields as $fieldname){
            if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])){
                $errors[] = $fieldname;
            }
        }

        if(empty($errors)){
            $name_field = Trim(stripslashes($_POST['name']));
            $name = explode(' ', $name_field);
            $firstname = ucfirst($name[0]);
            $email_field = Trim(stripslashes($_POST['email']));
            $message = Trim(stripslashes($_POST['message']));
            //
            $to = "[email protected]";
            $subject = "Email from Website";
            $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
            //
            mail($to, $subject, $body);
            echo "Thank You $firstname";
        }else{
            echo "Please complete all.";
        }

--- UPDATE ---

I've got part of it working now.

Part of the problem was the jquery couldn't find the php.

I created a 'code' folder inside wp-conntent and put the php there and the jquery looks like this.

    $j.post('wp-content/code/contactEngine.php', { theName:nameVal, theEmail:emailVal, theMessage:messageVal }, function(data){

Now I'm getting the returned data form the php file but the email isn't sent.

Will this not work in WP

    mail($to, $subject, $body);

Upvotes: 0

Views: 245

Answers (1)

user2191227
user2191227

Reputation: 412

You cannot use $_POST['name'] within Wordpress. Using other namings for input fields will fix your issue, eventually you could use the 'Contact Form 7' Wordpress-plugin for a better user experience :-)

Upvotes: 2

Related Questions