1

I've a very basic contact form on my Wordpress site (hard coded) and I can't get things to work. It works locally fine through XAMPP and I'm sure it's something I'm just missing but any help would be greatly appreciated. Thanks in advance!. I'm also using a template I created

<?php /* Template Name: contact */?>

<?php get_header(); ?>

<?php

//vars declared to store form input
$name=$email=$comment=$phone="";
//Error vars - to relay error message to the form 
$nameError=$emailError=$commentError="";
$error_message="";
$sentMessage="";
$status=0; //Will monitor if all fields have no errors and increment if so.

function sanitise_var($string){
    htmlentities($string);
    strip_tags($string);
    return stripslashes($string);
}

if(isset($_POST['submitted'])){

if($_POST['name']==""){ 
    $nameError="Please enter a name";
    $error_message="Oops, error in the form. Please check";

}

else {
    $name=$_POST['name'];
    ++$status;

}

if($_POST['email'] == "" || !preg_match("/^[a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", $_POST['email'])){ 
    $error_message="Oops, error in the form. Please check";
    $emailError="Please enter a valid email";

}

else{
    $email=$_POST['email'];
    ++$status;
}

if(!$_POST['phone']=="") $phone=$_POST['phone'];

if($_POST['comment']==""){ 
    $error_message="Oops, error in the form. Please check";
    $commentError="Please enter a message";
}

else{
    $comment=$_POST['comment'];  
    ++$status;

 }


}//submitted if statement


if($status==3 && $_POST['submitted']){ 
    $sentMessage="From: $name, email: $email, Phone: $phone, Comment: $comment";
    wp_mail("mathornley@gmail.com", "From Android Scoop contact form", $sentMessage);
    echo "Thanks, your email was sent successfully!";
}

else{

echo<<<SOQ

<div class="entry-content">         

    <h1 class="entry-title">Contact</h1>

        <p class="contact">
            If you have a query drop us a line using the form below. We're always  happy to hear from people with ideas for posts and content they'd like to           feature or maybe write about. Or maybe you just have some feedback you'd like to share with us. Why not just swing by and say hello. 
        </p>       
        <p class="requiring">* Denotes required fields</p>       

        <div class="form_left">     

    <form action="/contact/" method="POST">
                <p><label>Name:</label><input type="text" name="name" value="$name"/></p> 
                <p class="error">$nameError</p> 
                <p><label>Email</label><input type="text" name="email" value="$email"/></p>
                <p class="error">$emailError</p> 
                <p><label>Phone:</label><input type="text" name="phone" value="$phone"/></p> 
                <input type="hidden" name="submitted" value="yes"/>
                <input type="submit" value="Send your message"/>                        
        </div>

        <div class="form_right">        
                <p><label>Message:</label><br/><textarea name="comment" rows="20" cols="20">$comment</textarea></p>
                <p class="error">$commentError</p> 
            </form>
        </div>
</div>

SOQ;

}
?> 

<?php get_footer();?>
Mike Thornley
  • 401
  • 1
  • 9
  • 24
  • What's the error? Not working is so vague – Mr. Alien Mar 02 '13 at 22:00
  • Sorry..yeah... It checks the form ok but won't submit it and I get a page not found error. I'm wondering if it's due to wordpress as it works fine locally on my local server. – Mike Thornley Mar 02 '13 at 22:01
  • Mr Alien is correct - this should help http://stackoverflow.com/questions/4369/how-to-include-php-files-that-require-an-absolute-path – McNab Mar 02 '13 at 22:25
  • Nah it hasn't and I'm ok with PHP just not experienced enough I feel. I did try $file = realpath($_SERVER["DOCUMENT_ROOT"] . "/contact/"); and that too failed. I'm struggling. – Mike Thornley Mar 03 '13 at 14:31

3 Answers3

3

Try to use blank value for action like:

<form action="" method="POST">

If that doesn't work try renaming name parameter of the first input field to something else like:

<input type="text" name="myname" value="$name"/>
user850010
  • 6,131
  • 12
  • 35
  • 58
  • Nope.. I've tried all sorts and nothing appears to work. It's getting very frustrating. Here's the page http://androidscoop.co.uk/contact/, my last attempt was your suggestion.. – Mike Thornley Mar 03 '13 at 12:35
  • I added one more suggestion in above answer. I tried it in your web site and modification of "name" parameter for your first input field seemed to work. – user850010 Mar 04 '13 at 12:48
  • Superstar!. works perfect thanks very much but I'm not sure how that solved it?. I'm sorry due to my stupidness I can't vote you up!. – Mike Thornley Mar 11 '13 at 18:44
  • It looks like variable name of "name" was causing some conflict with WordPress. – user850010 Mar 11 '13 at 18:47
  • Ah I see. Many thanks for your time anyway!. I appreciate it. I'll see if anyone else has had the same issue. I thought it was the URL that was causing the issue perhaps when the form was finally submitted. Great stuff anyway. – Mike Thornley Mar 11 '13 at 18:50
2

Am not aware about the wordpress but as the general PHP rules go, and according to what you replied me in the comment, the error lies here

<form action="/contact/" method="POST">
              ----^----
Mr. Alien
  • 147,524
  • 33
  • 287
  • 271
  • Yeah I know the general rules around php forms, which is why locally all is ok as the script again runs to validate things once submitted. I have also tried posting to /contact/, the_permalink(); and the absolute url just to be sure and none work. Just seems odd how it works locally. I'm wondering if I'm missing something Wordpress related. – Mike Thornley Mar 02 '13 at 22:16
-1

Why won't you use some out-of-the-box contact form for WordPress? For example, Contact Form 7 is pretty good.

Contact Form 7 is an open source software and can manage multiple contact forms, plus you can customize the form and the mail contents flexibly with simple markup. The form supports Ajax-powered submitting, CAPTCHA, Akismet spam filtering and so on.

Installation

  • Upload the entire contact-form-7 folder to the /wp-content/plugins/ directory.
  • Activate the plugin through the ‘Plugins’ menu in WordPress.
  • You will find ‘Contact’ menu in your WordPress admin panel.
  • Create a contact form, copy it's url, paste it everywhere you want.
  • Contact form will appear exactly in it's location.

If you want an example, you can check out this site to see it's look & feel.

Johnny
  • 12,661
  • 14
  • 70
  • 112