WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Contact Form 404 Error (8 posts)

  1. skunkbad
    Member
    Posted 4 years ago #

    I have a simple contact form, and it could send email before my WP installation. The site didn't used to be a WP site.

    The contact form is on the contact page, and the action of the form is set to ../send_email.

    I get a 404 error when I get to the send_email page, but only if the script checks or tries to get anything from the $_POST array. I need help, because I can't go to sleep until I get this working, and it's 2:21AM!

    I have this on page.php:

    if(is_page('send_email') && !empty($_POST))
    	{
    		$to = 'myemail@email.com';
    		$from = (empty($_POST['email']))? 'myemail@email.com':$_POST['email'];
    		$message = (!isset($_POST['message']))? 'Request For More Information':$_POST['message'];
    
    		$headers = "From: $from\r\n";
    		$headers .= "Reply-To: $from\r\n";
    		$headers .= "Return-Path: $from\r\n";
    		$headers .= "X-Mailer: PHP mail on mydomain.com\r\n";
    
    		$message = "
    		Name: {$_POST['name']}
    		Phone: {$_POST['phone']}
    		Email: {$_POST['email']}
    
    		$message
    		";
    
    		$success = mail($to,'Contact From Website',$message,$headers);
    	}
  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Have you considered using a contact form plugin instead?

    Secure, Accessible Contact Form

    Contact Form 7

  3. skunkbad
    Member
    Posted 4 years ago #

    Actually, the contact form plugins seem pretty restrictive, and I came up with a super easy solution to my problem. It doesn't get any easier:

    1) Change action of form to outside WordPress
    2) Process the $_POST array and redirect user back into WordPress

    The .htaccess file in the root directory specifies that files that ARE located in the root directory, or directories that are located in the root directory, are accessible without being pushed through index.php

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    This was the key to setting up the form processing script. It just had to be located in the root directory. Simple, and because I'm not a WordPress genius, I did it with standard PHP. The sad thing is, it was so late at night when I was trying to figure this out, that I was losing sleep due to my foggy headedness.

    It was pretty amazing to see that this is a fairly common problem among WordPress users. A simple search through the forum for "$_POST problem" or similar will show this, but an easy solution is never offered, or at least not that I could find. This is the solution as far as I'm concerned. I'm not sure what WordPress is doing to the $_POST array, but I'm imagining that it has something to do with security.

  4. SpankMarvin
    Member
    Posted 4 years ago #

    I am aware that this is resolved, but there may be a more simple solution to this issue.

    I too decided to make my own form class in my theme, for extendability. I had this exact same issue, and then worked it out:

    Do not use the value 'name' as a value for the name attribute in a form field. So, if you have an input tag that has name="name", try instead using something like name="username"...

    Having changed that, by 404 errors disappeared on validation...

    John

  5. tbarsness
    Member
    Posted 4 years ago #

    @SpankMarvin is a genius. Works like a charm.

  6. Boone Gorges
    Moderator
    Posted 4 years ago #

    +1 SpankMarvin is a genius. I had a "name" problem too. Thought I was gonna go nuts.

  7. sareejoh
    Member
    Posted 4 years ago #

    @SpankMarvin Thank you thank you thank you!!!

  8. pancakes4breakfast
    Member
    Posted 4 years ago #

    I spent two days trying to solve this error and even did a clean wordpress install until I found this thread. Bless your heart, SpankMarvin!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags