0

This form code will not send emails...

This is the HTML Form:

<form method="post" action="index.php">
        <table class="form">
            <tr>
                <th class="label">
                    <label for="name">Name</label>
                </th>

                <td class="input">
                    <input type="text" name="name" id="name">
                </td>
            </tr>

            <tr>
                <th class="label">
                    <label for="email">Email</label>
                </th>

                <td class="input">
                    <input type="text" name="email" id="email">
                </td>
            </tr>

            <tr>
                <th class="label">
                    <label for="message">Message</label>
                </th>

                <td class="input">
                    <textarea type="text" name="message" id="message"></textarea>
                </td>
            </tr>
        </table>

        <input type="submit" value="Send">
    </form>

Here is the send code:

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";

$email_body = $email_body . "Name: " . $name;
$email_body = $email_body . "Email: " . $email;
$email_body = $email_body . "Message: " . $message;

mail("myemail@email.com" , "Contact Form" ,  $message , "From: $name");

header("Location: index.php?formsubmit=1");

exit;
}

?>

I'm not at all sure what is wrong, it worked before, but after I tried adding some things to my code, like a thing that said if form submit is equal to one display thanks for contacting us instead of explaining the form. That should not have anything to do with it though, I wouldn't think...

Could it be the fact that I added the whole ?formsubmit=1 thing?

Thanks for any help.

Hunter
  • 438
  • 1
  • 5
  • 16

2 Answers2

0

Make sure that you only set Location in header if you haven't put any code in front that would output any visible information (ie. echo, print_r...). Ideally header should be at the beginning of your page, before anything is 'printed'.

As explained in the PHP manual: PHP: header - Manual

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called.

A common practice, though, to verify if a form has been submitted is to name you submit button like this...

<input name="submit" type="submit" value="Send" />
<!-- I added the 'name' attribute here -->

and check it like this (in your 'send' code, as you called it)...

if (isset($_POST['submit'])) {
    // same index as the 'name' attribute above
}
benomatis
  • 5,536
  • 7
  • 36
  • 59
0

I figured it out myself,

<form method="post" action="index.php?formsubmit=1">

is what I needed, got rid of the

header("Location: index.php?formsubmit=1");

and stuck

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";

$email_body = $email_body . "Name: " . $name;
$email_body = $email_body . "Email: " . $email;
$email_body = $email_body . "Message: " . $message;

mail("jiggaboojones@gmail.com" , "Contact Form" ,  $message , "From: $name");

exit;
}

?>

At the bottom.

Thanks

Hunter
  • 438
  • 1
  • 5
  • 16