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

error message in two places

When I enter an invalid submission or a blank one I get the error message in both my error box AND the top of the page, Also, on my portfolio page I get an error message saying "Notice: Undefined variable: list_view_html in C:\xampp\htdocs\MCdocs\index.php on line 28" here is my code for both pages

index.php

 $pageTitle = "Mohr Creations | Portfolio";
 $section = "Portfolio";
 include('header.php'); ?>  



    <div id="content">



        <div class="section shirts latest">

            <div class="wrapper">

                <h2>Mohr Creations artwork</h2>

                <?php include("products.php"); ?> 
                <ul class="products">
                    <?php 

                    $total_products = count($products);
                    $position = 0;
                    $list_veiw_html = "";
                    foreach($products as $product_id => $product) {
                        $position = $position + 1;
                        if ($total_products - $position < 4) {
                            $list_view_html = get_list_view_html($product_id,$product) . $list_view_html;
                      }
                    } 
                    echo $list_view_html;
                   ?>

                </ul>

            </div>

        </div>

    </div>



   <?php include('footer.php'); ?> ```



contact.php




``` <?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = trim($_POST["name"]);
   $email = trim($_POST["email"]);
   $message = trim($_POST["message"]);

   if ($name == "" OR $email == "" OR $message == "") {
    $error_message = "You must specify a value for name, email address, and message.";

   }

   if(!isset($error_message)) {
       foreach( $_POST as $value ) {
        if(stripos($value, 'content-type:') !== FALSE){
            $error_message = "There was a problem with the information you entered.";

          } 
        }
      }
   if (!isset($error_message) AND $_POST["address"] != "") {
     $error_message = "Your form submission has an error.";

   }

   require_once("class.phpmailer.php");
   $mail = new PHPMailer();

   if (!isset($error_message) && !$mail->ValidateAddress($email)){
       $error_message = "You must specify a valid email address.";

   }

   if (!isset($error_message)) {

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

   $mail->SetFrom($email, $name);
   $address = "mohrcreations@cox.net";
   $mail->AddAddress($address, "Mohr Creations");
   $mail->Subject    = "Mohr Creations Contact form Submission |" . $name;
   $mail->MsgHTML($email_body);

      if($mail->Send()){
            header("Location: contact.php?status=thanks");
             exit;
        } else {
      $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;

   } }


   }
if (isset($error_message)) {
    echo $error_message;
}
?>

<?php
 $pageTitle = "Mohr Creations | Contact";
 $section = "contact";
 include('header.php'); ?>  

    <div class="section page">

        <h2 align="center">Contact</h2>

                   <?php if (isset($_GET["status"]) AND $_GET["status"]  == "thanks"){ ?>
                <p>Thanks for the email! we&rsquo;ll be in touch shortly.</p>
                <?php } else{ ?>

    <section id="primary">





        <?php 
            if (!isset($error_message)) {
                echo '<p>Mohr Creations is a family owned metal art business. We make all custom works of art by hand. 
        If you have a special request or want to know more please email us through our webstie!  </p>';
            } else {
               echo '<p class="message">' . $error_message . '</p>'; 
            }
        ?> 


  <form method="post" action="contact.php">

  <table>
    <tr>
        <th>
    <label for="name">Name</label>
     </th>
       <td>
        <input type="text" name="name" id="name">
       </td>
   </tr>
   <tr>
        <th>
    <label for="email">Email</label>
     </th>
       <td>
        <input type="text" name="email" id="email">
       </td>
   </tr>
    <tr>
        <th>
    <label for="message">Message</label>
     </th>
       <td>
        <textarea name="message" id="message"></textarea>
       </td>
   </tr>
   <tr style="display: none;">
        <th>
    <label for="address">Address</label>
     </th>
       <td>
        <input type="text" name="address" id="address">
        <p> Leave this field blank</p>
       </td>
   </tr>
       </table>
       <input type="submit" value="Send">

  </form>
<?php } ?>
   </div>
      <?php include('footer.php'); ?>

I have searched and searched for both problems and cant find a solution. I believe my index problem resides with $list_view_html = ""; I think that since it is blank it is causing an error but I looked trough the videos and have found no such value I have moved on past that step assuming in a later video we will assign that key a value. I hope you can help me I will keep skimming through the code I'm sure I just over looked a step in my contact.php but I cant find it.

2 Answers

Patricia Silva
PLUS
Patricia Silva
Courses Plus Student 89,123 Points

I'm not sure if this is your problem, but in your code above you have view misspelled.

$list_veiw_html = "";

YES! Thank you I can't believe I missed that that was my problem and as for my other Error I found that I hadn't deleted the bit that echos the error at the top of the page thank you again