Hey all,
I'm having a problem with my WordPress contact form. It is a script I edited from a tutorial I found, and it should be working right. I have a jQuery script that is supposed to help validate it and process it without page refresh.
The problem is that for some reason, neither the PHP script nor the jQuery script can seem to find my contact page when the form is submitted. The jQuery script, for example, works perfectly fine up until the ajax call, but once it sends the request Firebug says it hits the 404 page. I disabled JavaScript in my browser to see if it was the jQuery causing a problem, but the PHP script does the same thing. This doesn't make sense, because they're both on the contact page. When I click "submit", the script sends me to my 404 page. The form is at www.mydomain.com/contact, and the form action is also www.mydomain.com/contact. So why would I get the 404 page when it submits?
I've used an almost identical script on another WordPress site I made for a client, and it works perfectly. I am hosted by Dreamhost, if that makes a difference.
Here is the contact page script:
contact.php
<?php
/*
Template Name: contact
*/
//If the form is submitted
if(isset($_POST['submit'])) {
$error = array();
// Check to make sure that the name field is not empty
if(trim($_POST['name']) === '') {
$nameError = true;
$hasError = true;
} else {
$name = trim($_POST['name']);
}
// Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) === '') {
$emailError = true;
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$emailError = true;
$hasError = true;
} else {
$email = trim($_POST['email']);
}
// Check to make sure that the subject field is not empty
if(trim($_POST['subject']) === '') {
$subjectError = true;
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
// Check to make sure comments were entered
if(trim($_POST['message']) === '') {
$messageError = true;
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$message = stripslashes(trim($_POST['message']));
} else {
$message = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail@mydomain.com';
$subject = $subject;
$body = "Name: $name \n\nEmail: $email \n\nMessage: $message";
$headers = 'From: mydomain.com <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
} ?>
<?php get_header(); ?>
<div id="main">
<div id="contact-left">
<div id="contact-container">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1>Contact Me</h1>
<?php the_content(); ?>
<form id="contactme" action="<?php the_permalink(); ?>" method="post">
<fieldset>
<label for="name" class="error"<?php if($nameError) echo ' style="display: block;"'?>>Please provide a name</label>
<input type="text" name="name" id="name" size="40" maxlength="50" value="name" onfocus="if(this.value=='name') this.value='';" onblur="if(this.value=='') this.value='name';" />
<label for="email" class="error"<?php if($emailError) echo ' style="display: block;"'?>>Please provide a valid e-mail address</label>
<input type="text" name="email" id="email" size="40" maxlength="50" value="email" onfocus="if(this.value=='email') this.value='';" onblur="if(this.value=='') this.value='email';" />
<label for="subject" class="error"<?php if($subjectError) echo ' style="display: block;"'?>>Please provide a subject</label>
<input type="text" name="subject" id="subject" size="40" maxlength="150" value="subject" onfocus="if(this.value=='subject') this.value='';" onblur="if(this.value=='') this.value='subject';" />
<label for="message" class="error"<?php if($messageError) echo ' style="display: block;"'?>>Please provide a message</label>
<textarea name="message" id="message" rows="8" cols="43" onfocus="if(this.value=='message') this.value='';" onblur="if(this.value=='') this.value='message';" >message</textarea>
<input type="submit" id="submit" value="submit" />
</fieldset>
</form>
<?php endwhile; endif; } ?>
</div>
</div> <!-- /contact-left -->
<?php get_sidebar(); ?>
</div> <!-- /main -->
<div class="clear"></div>
<?php get_footer(); }?>
Thanks!