Jump to content

Contact form won't work in Wordpress :(


Lucy

Recommended Posts

I'm using the same contact form code (not written by me, as I don't understand PHP very much) that I have on my Portfolio website. It definitely works there. However, it won't on a Wordpress site I'm working on. You fill in the fields, submit, and it simply comes up with the message about no posts being found - I take that to mean it can't find the page, except it's not meant to GO to the PHP file, it's meant to use it... I noticed the search bar originally had a messed up address for the PHP file on submitting originally, so I've altered it so it should use the correct link, and the address bar now shows 'contact.php', the original page the form is submitted on. But with the same error - and no message is sent at any point. I'll post the code below. I don't really understand what's going on at all, would appreciate any help.

 

Contact form:

<form action="<?php get_template_directory_uri() . '/contact-form-handler.php' ; ?>" method="POST" name="contactform">		<label for="name">Name*:</label>		<input type="text" name="name" required>		<label for="email">Email*:</label>		<input type="email" name="email" required>		<label for="message">Message*:</label>		<textarea required></textarea>		<input type="submit" value="Send">		</form>

contact-form-handler.php:

<?php $errors = '';$myemail = 'admin@*************k';	if(empty($_POST['name'])  ||    empty($_POST['email']) ||    empty($_POST['message'])){    $errors .= "n Error: all fields are required";}$name = $_POST['name']; $email_address = $_POST['email']; $message = $_POST['message']; if (!preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $email_address)){    $errors .= "n Error: Invalid email address";}if( empty($errors)){	$to = $myemail; 	$email_subject = "Contact form submission: $name";	$email_body = "You have received a new message. ".	" Here are the details:n Name: $name n Email: $email_address n Message: $message"; 		$headers = "From: $myemailn"; 	$headers .= "Reply-To: $email_address";		mail($to,$email_subject,$email_body,$headers);	//redirect to the 'thank you' page	header('Location: *************/thanks/');} 	?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head>	<title>Contact form handler</title></head><body><!-- This page is displayed only if there is some error --><?phpecho nl2br($errors);?></body></html>

If you need any more information please say.

Edited by Lucy
Link to comment
Share on other sites

Put this at the top of your handler to see what's in the post array:Var_dump($_POST);Exit;Is everything there?

Edited by niche
Link to comment
Share on other sites

Something's keeping your Post from posting data. Try posting a simpler form from a new script. Does it var_dump data? If it does, there's something else in the script, with the original form, that's probably causing the problem.

Edited by niche
Link to comment
Share on other sites

<textarea required></textarea> does not have name attribute value of message, so it should not get pass validation, and you probably should have error appear stating "index of 'message' does not exist"

I've changed it to have both name and value set to 'message' - however, still doesn't work.

 

Niche -

This was really the simplest I could find. I don't think there's a problem with the form handler itself, because I'm using it on another (non-Wordpress) site and it works fine there.

Link to comment
Share on other sites

Is there a way to tell whether the form handler file is actually being used?

 

Edit - Okay, tested it using an invalid email address. It isn't being used at all.

 

Edit 2 - ... The wordpress code for the url wasn't working the way I thought it should. That was the problem. It's still not working, but I'm actually getting error messages now. I'll keep you posted ( ;) post? Get it? Haha - I'm far too tired for this)...

 

Edit 3 - It works! :D Many thanks for all the help.

Edited by Lucy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...