Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP Form

Hello everyone,

I'm creating a form but i'm having trouble adding additional fields like first name and last name etc. The form is passing the subject, email and first name only.

Here's the source code:

<?php

} else {  // the user has submitted the form
  // Check if the "from" input field is filled out
  if (isset($_POST["from"])) {
    // Check if "from" email address is valid
    $mailcheck = spamcheck($_POST["from"]);
    if ($mailcheck==FALSE) {
      echo "Invalid input";
    } else {
      $from = $_POST["from"]; // sender
      $subject = $_POST["subject"];
      $message = $_POST["message"];        
      $fname = $_POST["fname"];
      $lname = $_POST["lname"];
      // message lines should not exceed 70 characters (PHP rule), so wrap it
      $message = wordwrap($message, 70);       



      // send mail
        mail("user@email.com", $subject, $message, $fname, $lname, "From: $from\n");


       echo "Thank you for sending us feedback";

I edited your post to help with the syntax highlighting. You can check out the Markdown Cheatsheet for a reference on that, if you'd like.

6 Answers

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

Noel,

It looks like you're trying to send too many arguments through the php mail() function. The correct syntax is as follows:

mail(to, subject, message, headers, parameters)

(headers and parameters optional)

Instead of comma-separating all of your user's inputs and passing them as separate arguments the way you're doing now, you'll need to clump a lot of them into a single variable for the email message's content.

Try this (I haven't tested this code, but I think it should work):

$from = $_POST["from"]; // sender
$subject = $_POST["subject"];       
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$title = $_POST["title"];
$website = $_POST["website"];         
$address1 = $_POST["address1"];    
$address2 = $_POST["address2"];        
$city = $_POST["city"];
$state = $_POST["state"];        
$zip = $_POST["zip"];        
$pnumber = $_POST["pnumber"];        
$ext = $_POST["ext"];         

$message = "Name: " . $fname . " " . $lname . "\n\n";
$message .= "Title: " . $title . "\n\n";
$message .= "Website: " . $website . "\n\n";
$message .= "Address: \n\n";
$message .= $address1 . "\n";
$message .= $address2 . "\n";
$message .= $city . ", " . $state . " " . $zip . "\n\n";
$message .= "Phone: " . $pnumber;
if($ext != '') { $message .= " ext. " . $ext; }
$message .= "\n\n";

// send mail

mail("user@email.com", $subject, $message, "From: user@email.com \r\n Reply-To: $from");

Note that for the headers of your email, I changed the "From:" email address. I think most hosting services will require you to send php emails from your domain. You can, however, have a "Reply-To" email address outside of your domain. This should give you the same functionality when replying to user submissions.

Hopefully this helps.

Thank you! :)

Ninja-edited for the highlighting. I didn't change any of it except for wrapping it in backticks; it may have been garbled because of the tags.

Just for future reference, the backtick key is above your tab key, right next to 1.

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

Noel,

I see a couple of issues, but it's really tough to decipher your code right now. If you edit your post (and wrap the code correctly) so it's easier to troubleshoot, I'll take another look.

The problems I see right now:

You're pulling in the wrong values for some of your variables. Right now you have:

$message = $_POST["message"];
$fname = $_POST["message"];
$lname = $_POST["message"];

This should read:

$message = $_POST["message"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];

Also, for a lot of your inputs, you have type set to things like "lastwebsite", "lasttitle", "lastname" which are not valid input types. You'll probably want to change all of these to "text".

Let me know if you get the post reformatted and I'll take another look.

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

You should also check the documentation for the PHP mail() Function to make sure you're getting that syntax right. It looks like you're misusing $fname and $lname in your mail function.

Shawn,

I have updated the form. you view the entire source code here http://noelg.com/phpform.html

The form only post up to the lname and ignores everything else.

I'm not able to display the source code on the post.

Everything works fine until the Phone message.

$message . = "Phone: " . $pnumber; if($ext != '') { $message .= " ext. " . $ext; } $message .= "\n\n";

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

Oops! Remove the space between the first . and = and it should work. Will edit my answer, too.