1

When the submit button is clicked, the browser goes to www.****.com/contact.php and the page is blank. The email is also not delivered. This is my first time dealing with php. What am I missing?

Here is the form:

<form class="comment-form" action="contact.php" method="POST">
 <p class="comment-notes">Your email address will not be published. All fields are required.</p>
 <p class="comment-form-email">
 <label for="author">Name</label>
 <span class="required">*</span>
 <input id="author" type="text" class="input-text" name="name">
 </p>
 
    <p class="comment-form-author">
 <label for="email">Email</label>
 <span class="required">*</span>
 <input id="email" type="text" class="input-text" name="email">
 </p>
 <p class="comment-form-url">
 <label for="subject">Subject</label>
 <span class="required">*</span>
 <input id="subject" type="text" class="input-text" name="subject">
 </p>
 <p class="comment-form-comment">
 <label for="message">Message</label>
 <textarea name="message" id="message" cols="45" rows="10" class="input-text"></textarea>
 </p>
 <p class="form-submit">
 <input class="btn btn-md btn-default" name="submit" type="submit" id="button" value="Send"><input type="reset" value="Clear">
 </p>
</form>

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$formcontent = "From: $name\n Message: $message";
$recipient = "me@example.com";
$subject = "$subject";
$mailheader = "From: $email \r\n";
error_reporting(E_ALL);
ini_set(display_errors, 1);

mail($recipient, $subject, $formcontent, $mailheader) or die ("Error!");
echo "Thank You! We will respond to your inquiry as soon as possible"; " -"<a href='contact.html' style='text-decoration:none;color:#ff0099;'> "Return Home"</a>;

?> 
Patrick Bentley
  • 462
  • 1
  • 5
  • 17
  • 4
    possible duplicate of [PHP's white screen of death](http://stackoverflow.com/questions/1475297/phps-white-screen-of-death) – ChrisGPT was on strike Feb 11 '15 at 20:48
  • 2
    That last `echo` looks weird. Edit: also: `$message` and `$subject` are never defined –  Feb 11 '15 at 20:51
  • Put the PHP in the file that you used as the 'action' of your form. You use two separate files. One is the HTML form. The other is the PHP that receives the data from the form. Also, be prepared to have this form used for spam injection. – kainaw Feb 11 '15 at 20:53

5 Answers5

2

It looks like this form is posting to itself? IF that's the case, I think you should probably be using

<form class="comment-form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

htmlspecialchars() is a safety thing. Helps to prevent against some hacks.

Coder-guy
  • 414
  • 6
  • 16
  • What kind of hacks can `htmlspecialchars()` prevent? –  Feb 11 '15 at 20:56
  • 1
    http://www.w3schools.com/php/php_form_validation.asp htmlspecialchars() will help against XSS attacks – Coder-guy Feb 11 '15 at 20:58
  • 1
    But `htmlspecialchars()` is only needed if you use PHP_SELF in your action. I'd personally just use `action="contact.php"`. –  Feb 11 '15 at 21:01
  • @jared If possible, please avoid the use of w3schools as reference. – John Weisz Feb 11 '15 at 21:03
  • If I recall correctly, it'll help prevent people from sending HTML code to your server through the fields by rendering the typical HTML tags useless. I've seen people manipulate MySQL databases that were not protected as well, and people simply sent commands to the website's database because nothing was trying to validate and protect what was being sent to the server. It's a diff command for MySQL, but it's the same concept of not allowing people to inject code. – Fata1Err0r Feb 11 '15 at 21:09
  • Yeah, but I think only neccessary when you use PHP_SELF, where people can "trick" the form. There's an example of it on that w3schools page. `htmlspecialchars()` in a form action is NOT needed, when setting the action to an existing file (contact.php) –  Feb 11 '15 at 21:15
1

Looks like your PHP isn't being called properly because it isn't being triggered by the submit button. The code below executes when the submit button is pressed.

if(isset($_POST['submit']
{
  // put your PHP code here, this executes when submit is...submitted
}

Give this a shot, it should help you out some.

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

if(isset($_POST['submit']))
{
    //Gather the POST info and set them to variables
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    // Setup the message and define who will be emailed the info
    $formcontent = "From: $name\n Message: $message";
    $recipient = "pbentley07@gmail.com";
    $headers = 'From:' . $email;


    mail("$recipient", $subject, $formcontent, $headers) or die ("Error!");
    echo "Thank You! We will respond to your inquiry as soon as possible! - <a href='contact.html' style='text-decoration:none;color:#ff0099;'>Return Home</a>";
}


?> 

        <form class="comment-form" action="contact.php" method="POST">
            <p class="comment-notes">Your email address will not be published. All fields are required.</p>
            <p class="comment-form-email">
            <label for="author">Name</label>
            <span class="required">*</span>
            <input id="author" type="text" class="input-text" name="name">
            </p>

            <p class="comment-form-author">
            <label for="email">Email</label>
            <span class="required">*</span>
            <input id="email" type="text" class="input-text" name="email">
            </p>
            <p class="comment-form-url">
            <label for="subject">Subject</label>
            <span class="required">*</span>
            <input id="subject" type="text" class="input-text" name="subject">
            </p>
            <p class="comment-form-comment">
            <label for="message">Message</label>
            <textarea name="message" id="message" cols="45" rows="10" class="input-text"></textarea>
            </p>
            <p class="form-submit">
            <input class="btn btn-md btn-default" name="submit" type="submit" id="button" value="Send"><input type="reset" value="Clear">
            </p>
        </form>
Fata1Err0r
  • 836
  • 1
  • 6
  • 14
  • The PHP is ALWAYS called, even if no submit button is triggered. –  Feb 11 '15 at 20:57
  • yes @IkoTikashi, the form submits by itself when the page loads. – Joe Kdw Feb 11 '15 at 21:00
  • 1
    @JeanGkol Nope, the form doesn't submit itself when the page loads. User needs to click the submit button to submit the form. That's why it's a good idea to use the code Fata1Err0r showed. –  Feb 11 '15 at 21:04
  • yes, you're right @IkoTikashi :D - ,,,,,. even if no submit button is triggered – Joe Kdw Feb 11 '15 at 21:06
0

Could you confirm that the page+code you posted here is contact.php file?

Try to fix your php code first, this line:

echo "Thank You! We will respond to your inquiry as soon as possible"; " -"<a href='contact.html' style='text-decoration:none;color:#ff0099;'> "Return Home"</a>;

must be:

echo "Thank You! We will respond to your inquiry as soon as possible";
?>
<a href="contact.html" style="text-decoration:none;color:#ff0099;">Return Home</a>
<?php
Alex
  • 16,739
  • 1
  • 28
  • 51
0

in your echo statement, you have bad syntax. Try

echo "Thank You! We will respond to your inquiry as soon as possible - <a href='contact.html' style='text-decoration:none;color:#ff0099;'>\"Return Home\"</a>";
xjx424
  • 173
  • 2
  • 12
0

You have some serious "errors" in your PHP:

$message is not defined

$subject is not defined

It's ini_set('display_errors', 1);, not ini_set(display_errors, 1);

Your echo line is invalid, remove the additional HTML part

Your final code should looke like this:

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

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent = "From: $name\n Message: $message";
$recipient = "me@example.com";
$mailheader = "From: $email \r\n";

mail($recipient, $subject, $formcontent, $mailheader) or die ("Error!");
echo "Thank You! We will respond to your inquiry as soon as possible";
?>
<a href='contact.html' style='text-decoration:none;color:#ff0099;'> "Return Home"</a>

You might also want to read up on form validation