3

I have a simple php page with an html form. Its set up to let you fill out the form and automatically send an email to my email. Instead of this happening, I don't get any email and the page redirects to a "File not found (404 error)".

Here's the PHP

<?php

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

    //Name
    $Name = $_POST['Name'];
    $Name = mysqli_real_escape_string($Name);

    //Email
    $Email = $_POST['Email'];
    $Email = mysqli_real_escape_string($Email);

    //Subject
    $Subject = $_POST['Subject'];
    $Subject = mysqli_real_escape_string($Subject);

    //Message
    $Message = $_POST['Message'];
    $Message = mysqli_real_escape_string($Message);

    $email_subject = $Subject . ' from: ' . $Email;
    $email_from = $Email;
    $email_body = $Message;

    $webEmail = "[email protected]";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $Email \r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";



    mail($webEmail,$email_subject,$email_body,$headers);
}
?>

Here's the html form

<form action="action_page.php" name="myform">
            <input onfocus="this.value='';" type="text" name="Name" value="Name" id="name">
            <input onfocus="this.value='';" type="text" name="Email" value="Email" id="email">
            <input onfocus="this.value='';" type="text" name="Subject" value="Subject" id="subject">
            <textarea onfocus="this.value='';" cols="50" rows="4" placeholder="Message" name="Message" id="message"></textarea>
            <input type="submit" value="submit" id="submit">

        </form>

Any ideas?

6
  • You are working on localhost.? Commented Apr 4, 2015 at 21:10
  • No, this is live on my website (using godaddy to host the website if that's important). If you'd like to see the contact form in action the link is www.lizbethmcgee.com/pages/Contact.php Commented Apr 4, 2015 at 21:13
  • try error_reporting_all to get the errors and also after sending if you are not directing to any page with any message then it will show blank page. Commented Apr 4, 2015 at 21:15
  • I got your problem. you are sending all your data in query string and using $_POST to get them how's its possible? Commented Apr 4, 2015 at 21:17
  • 2
    Why you are using weird mysqli_real_escape_string function if you are not sending your data to sql? Commented Apr 4, 2015 at 21:37

2 Answers 2

5

There are a few things wrong here.

Let's start from the top and with your conditional statement (and work our way down).

if (isset($_POST['submit'])){...}

Your submit button <input type="submit" value="submit" id="submit"> does not have a name attribute for it.

  • You may be relying on an "id" alone, which won't work, it needs the "name" attribute.

Modify it so that it reads as:

<input type="submit" name="submit" value="submit" id="submit">
  • Since it's not named, anything inside that conditional statement will not execute.

Then you're using mysqli_real_escape_string() which theoretically requires the connection be passed to that function.

I.e.:

$Name = mysqli_real_escape_string($connection,$Name);

and do the same for the rest; assuming you are interacting with a database.

However, it's unclear whether you are indeed interacting with your database or not, so if you're not interacting with it, remove all of those mysqli_real_escape_string from your POST arrays.

I.e.:

$Name = $_POST['Name'];

and do the same for the others.

You're also missing POST as the method in your form:

<form action="action_page.php" name="myform">

which defaults to GET when omitted. Therefore, change it to:

<form action="action_page.php" name="myform" method="post">

To then check if mail has been sent, change

mail($webEmail,$email_subject,$email_body,$headers);

to

if(mail($webEmail,$email_subject,$email_body,$headers)){
echo "Sent";
}

If you see "Sent", then mail would have done its job. If no mail is received, check your Spam. Or, your server could be blacklisted.

Make sure that you do have access to using mail().

You should also make sure that all inputs have been filled using a conditional empty().

I.e.:

if(!empty($_POST['Name'])){
// $Name = mysqli_real_escape_string($Name);
   $Name = $_POST['Name'];
   }

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

I don't know what these are used for onfocus="this.value='';" so, if you've got some JS/Ajax happening somewhere, you'll need to check those.

  • If they're not used for anything, you can remove those.

You should also try removing value="Name" and for the others as well, just in case.

Sidenote:

If you're still getting a 404 error, then try putting your PHP on top and your HTML form below that, and inside the same file with a .php extension, and doing:

<form action="" name="myform" method="post">

using action="" as the action to take place on the same page.


Additional footnote:

I noticed in a comment you placed in another answer:

"I added header( 'Location: lizbethmcgee.com/pages/Contact.php'; ) ;"

First, make sure you're not outputting before header.

Plus, you should also include a full http:// call and exit;:

header("Location: http://www.lizbethmcgee.com/pages/Contact.php");
exit;

to avoid further execution of code that may be present below that, should there contain any.


As AdamT stated in a comment:

Sending a mail with the php mail command, there is no guarantee that the mail will arrive to the inbox you send it to. As mentioned, there are lots of spammers out there, and unfortunately, a heavy degree of spam filtering. If you really want to have a better chance at getting the mail delivered, you should contact GoDaddy and ask them how you can set up SMTP emailing for your account.

Sign up to request clarification or add additional context in comments.

3 Comments

Sending a mail with the php mail command, there is no guarantee that the mail will arrive to the inbox you send it to. As mentioned, there are lots of spammers out there, and unfortunately, a heavy degree of spam filtering. If you really want to have a better chance at getting the mail delivered, you should contact GoDaddy and ask them how you can set up SMTP emailing for your account.
@AdamT Thanks for the comments Adam, it's a very good idea. I've added it in the answer, quoting you.
Fred - ii -Thanks. I like this Discussion here, lots of participation - should be very helpful to O.P.
0

As mentioned above, your submit button MUST have an attribute name="submit" or else your if (isset($_POST['submit'])){... will not work. However, I was having this exact issue recently and somehow stumbled onto a mention that <input name="name"> was to blame. In your case the <input name="Name">. Once I updated the name attribute to 'username', the form functioned perfectly. Although I haven't been able to find the precise reasoning for this quirk, it definitely raised awareness of what I was naming my attributes, selectors, variables, etc...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.