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

link wants to open php file instead of going to webpage?

working on a site for a someone and I did a email form page with php and when I go to the click on the button to go to the form.php page it wants to save it as a file instead of going to the webpage? it is saved as form.php as well.

// message vars
$msg = '';
$msgClass='';

// check submit
if(filter_has_var(INPUT_POST,'submit')){

  // get form data
  $name = htmlspecialchars($_POST['name']);
$email= htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
$subject = htmlspecialchars($_POST['subject']);

// check reqiured fields
if(!empty($email) && !empty($name) && !empty($message)){

  if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
  $msg = 'OH NO! Please use vaild email';
  $msgClass = 'card-panel red';
  }else{
    // passed
    // toemail
    $toEmail = 'mike.fazek@gmail.com';
    $subjectMsg = $subject;
    $body = '<h2>Contact Request from site</h2>
      <h4>Name</h4><p>'.$name.'</p>
      <h4>Email</h4><p>'.$email.'</p>
      <h4>Message</h4><p>'.$message.'</p>
    ';
    // headers
    $headers = "MIME-Verison 1.0" ."\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
    // additonal from
    $headers .= "From: " .$name. "<" .$email. ">" "\r\n";

    if(mail($toEmail, $subject, $body, $headers)){
      $msg = 'YAY! We will be in touch with you Soon!';
  $msgClass = 'card-panel green';
    }else{
      $msg = 'Oops... email was not sent :(';
  $msgClass = 'card-panel red';
    }
  }
}else{
  // failed
  $msg = 'Oops... Please Fill in all fields';
  $msgClass = 'card-panel red';
}

}


?>```

```<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- Logo for Title bar -->
  <link rel="shortcut icon" href="./images/tcLogoTitleBar.png">
  <title>Taylor's Custom Homes</title>
   <!-- Compiled and minified CSS -->
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

   <!-- Compiled and minified JavaScript -->
   <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

  <!-- external stylesheet css -->
  <link rel="stylesheet" href="../style.css">

</head>
<body id="contact-bgk">
  <a href="index.html"> <img src="./images/tcLogoTitleBar.png" alt="Taylor's Custom Homes" class="logo-image"></a>
  <!-- form -->
<div class="contact-container">
  <h2 class="contact-title">Contact Us</h2>
  <div class="row">
    <?php if($msg != ''):?>
       <div class=" <?php echo $msgClass;?>"><?php echo $msg; ?></div>
      <?php endif; ?>
    <form action="form.php" autocomplete="on" method="POST"  class="col s12">
      <div class="row">
        <div class="input-field col s6">
          <input  id="first_name" type="text" class="validate" name="name" value="<?php echo isset($_POST['name'])? $name : ''; ?>
          ">
          <label for="first_name">First Name</label>
        </div>
        <div class="input-field col s6">
          <input id="last_name" type="text" class="validate" name="name" value="<?php echo isset($_POST['name'])? $name : ''; ?>">
          <label for="last_name">Last Name</label>
        </div>
      </div>

      <div class="row">
        <div class="input-field col s12">
          <input id="email" type="email" name="email" class="validate" value="<?php echo isset($_POST['email'])? $email : ''; ?>">
          <label for="email">Email</label>
        </div>
        <div class="row">

          <div class="input-field col s12">

          <input id="subject" type="text" name="subject" class="validate" value="<?php echo isset($_POST['subject'])? $subject : ''; ?>">

          <label for="subject">Subject</label>

        </div>
      </div>

  <div class="row">
      <div class="row">
        <div class="input-field col s12">
          <textarea id="message" type="text" class="materialize-textarea" name="message"></textarea>
          <label for="textarea1">Message</label>
        </div>
      </div>
    </form>
  </div>

    </form>
    <button class="btn waves-effect waves-light amber" type="submit" name="submit" value="Send">Submit
    </button>
  </div>
</div>```

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Michael Fazekas! I'd be interested in some details about your server. First, what version of PHP is it running. Secondly, are you receiving any notices, warnings, or errors in the php_error.log file?

Let me know! :sparkles:

edited for additional comment

It sounds like the server isn't running. That's the only reason I know that would reliably produce this behavior. Check to make sure your instance of Apache (or whatever server software you're running) is functional and running.

Hey Jennifer! I'm using MAMP and it's running 7.4.1 it was working fine until I finished the mail function once I take all the php code out it work brings up everything from my html code when i put it back in it goes back to a white screen. i took out certain parts of the code to see what would happen it all goes to a white screen unless its php code is completely taken out/deleted. kind of new to php so im not 100% what to do for a error log just yet. looking to figure it out.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Michael Fazekas MAMP should be generating a php_error.log file inside your MAMP folder under "logs". If you're getting a blank white screen, it most likely means there's a syntax error and I think I found it. You have this line:

$headers .= "From: " .$name. "<" .$email. ">" "\r\n";

But there's a concatenation operator missing between the last two strings. I feel pretty sure you mean:

$headers .= "From: " .$name. "<" .$email. ">" . "\r\n";  // note the . before the "\r\n"

Hoping this helps! But definitely find that php_error.log file. You should be able to search your computer for it :smiley:

I'm coming across a Parse error: syntax error, unexpected token "else" on line 45? tried googling it but still no clear answer to me i've tried a few different things still nothing.. the last else statement in the php code..

<?php
// message vars
$msg = '';
$msgClass='';

// check submit
if(filter_has_var(INPUT_POST,'submit')){

  $name = $_POST['name'];
  $email= $_POST['email'];
  $message = $_POST['message'];
  $subject = $_POST['subject'];


  if(!empty($email) && !empty($name) && !empty($message)){

    if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
    $msg = "OH NO! Please use vaild email";
    $msgClass = "card-panel red";
    }
    else{
      $toEmail = "mike.fazek@gmail.com";
      $subjectMsg = $subject;
      $body = "<h2>Contact Request from site</h2>
        <h4>Name</h4><p>'.$name.'</p>
        <h4>Email</h4><p>'.$email.'</p>
        <h4>Message</h4><p>'.$message.'</p>
      ";
    }

    $headers = "MIME-Verison 1.0" ."\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8" ."\r\n";

    // additonal from
    $headers .= "From: " .$name. "<" .$email. ">" . "\r\n";

            if(mail($toEmail, $subject, $body, $headers)){
              $msg = "YAY! We will be in touch with you Soon!";
          $msgClass = "card-panel green";
            }
            else{
              $msg = "Oops... email was not sent :(";
          $msgClass = "card-panel red";
            }
            else{
            // failed
            $msg = "Oops... Please Fill in all fields";
            $msgClass = "card-panel red";
          }
        }
      }

?>
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Michael Fazekas! At the bottom you have an if else else statement. You can have an if elseif else or an if and else, but there's one too many else clauses :smiley: This is going to cause an error.

I'm expecting something like:

if($first_condition) {
  // whatever you want to do if that if is true
}elseif ($second_condition) {
  // whatever you want to do if the second condition is true
} else {
  // the thing you want to do if neither of the above were true
}

or

if($first_condition) {
   // if this was true
} else {
   // if it wasn't true
}

But the following is invalid:

if($first_condition) {
   // if this was true do stuff here
} else {

} else {

}

You can't have two else blocks tied to a single if :smiley: