2

I am having a difficult time with the syntax of PHP when trying to customize the layout of content that is sent using a PHP form. I am trying to display each input on a different line in an email... Below is my PHP code:

    <?php
// Section 1.
if( $_POST['name_here_goes'] == '' ){

    // Section 2.
    if ( !empty($_POST['firstName']) && !empty($_POST['lastName']) && !empty($_POST['emailAddress']) ) {

        $to         = '####@############.com';
        $subject     = 'NEW Contact Form';
        $message     = $_POST['firstName'] "\n\n" $_POST['lastName'];
        $headers     = 'From: ' . $_POST['emailAddress'] . ' ' . "\r\n" .
                      'Reply-To: ' . $_POST['emailAddress'] . '' . "\r\n" .
                      'X-Mailer: PHP/' . phpversion();

        // Section 3.
        if ( mail($to, $subject, $message, $headers) ) {
            echo 'Email sent. Congrats!';
        }
    }else{
        echo 'Please fill all the info.';
    }

}else{

     // Section 4.
     echo 'Spam detected!';

}

Here is my HTML code:

    <form name="contact" method="post" action="sell.php">
    <div>
        <input type="text" name="firstName" value="" placeholder="First Name" />
        <input type="text" name="lastName" value="" placeholder="Last Name" />
        <input type="text" name="emailAddress" value="" placeholder="Email" />

    </div>
    <div>
        <input type="text" class="robotic" name="name_here_goes" value="" />
        <input type="submit" name="submit" value="Submit"/>
    </div>
</form>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Nick
  • 1,471
  • 2
  • 21
  • 35
  • Constructing valid mail is non-trivial in it's most basic form and highly complex once you get beyond the simple use cases. I would highly recommend using a pre-existing library for this, eg. PHPMailer or SwiftMailer. Both are awesome, well-tested, highly reliable libraries. There are probably others as well, but these are just the two I've personally used. – JamesG Nov 24 '15 at 23:02
  • This is a very simple form.. I'm not sure i need to do that? – Nick Nov 24 '15 at 23:03
  • @Fred-ii- Sorry i updated with the current code... – Nick Nov 24 '15 at 23:04
  • I posted my solution below @Nick – Funk Forty Niner Nov 24 '15 at 23:23
  • ok... so........ where are we here? didn't run off with my answer, did you? haha – Funk Forty Niner Nov 24 '15 at 23:38

1 Answers1

0

You see this line:

$message     = $_POST['firstName'] "\n\n" $_POST['lastName'];

It's missing concatenates for it, between your POST arrays and having error reporting set to catch and display, would have thrown you:

Parse error: syntax error, unexpected '"\n\n"' (T_CONSTANT_ENCAPSED_STRING) in /path/to/file.php on line x

Change it to:

$message     = $_POST['firstName'] . "\n\n" . $_POST['lastName'];
                                   ^        ^ added

However, you will need to work on your headers a bit and you shouldn't be passing your POST arrays directly in like that, you may get hit by an XSS injection.

References:

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: Displaying errors should only be done in staging, and never production.


Plus, as stated in comments. PHPmailer/Swiftmailer are already setup for you to use and work very well for handling plain text and HTML formatted mail.


Edit:

"Sorry for the delayed response.. That worked! But how would you get it to direct to a different page after submission? – Nick"

You would use a header http://php.net/manual/en/function.header.php and replacing the echo 'Email sent. Congrats!'; with the header, you can't use both.

    if ( mail($to, $subject, $message, $headers) ) {
      header('Location: http://www.example.com/');
      exit;
    }

Example from the manual:

header('Location: http://www.example.com/');
exit;

Make sure you are not outputting before header. If you do get a notice, consult:

You can also consult the following for more redirection methods:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Sorry for the delayed response.. That worked! But how would you get it to direct to a different page after submission? – Nick Nov 25 '15 at 15:58