How to Build a Contact Page on a PHP Template

Contact pages for websites sometimes include other elements, maybe a form or another way to interact. This means you might need to include another JavaScript file or different CSS. Luckily, you can do so by extending the PHP templating class and using that type property. Follow these steps to create the Contact page:

  1. Open classPage.php.

  2. Inside of the _getHead() method, add a conditional for a new type of page.

    The entire _getHead method should look like this:

    protected function _getHead() {
        $output = "";
        if ($this->type == "contact") {
            $output .= file_get_contents("pageHeadContact.txt");
        } else {
            $output .= file_get_contents("pageHead.txt");
        }
        if ($this->titleExtra != "") {
            $title = $this->titleExtra . "|" . $this->title;
        } else {
            $title = $this->title;
        }
        $output .= "<title>" . $title . "</title>";
        $output .= "</head>";
        return $output;
    } //end function _getHead()

    This code checks to see if the type property ($this->type) is set to contact. If it is, then a new <head> section file is included. Otherwise, the normal <head> section is included.

  3. Save classPage.php.

  4. Create a new empty file in your text editor.

  5. Inside of the file, add the following markup:

    <head>
    <link rel="stylesheet" href="style.css" type="text/css" />
    <link rel="stylesheet" href="contact.css" type="text/css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  6. Save the file as pageHeadContact.txt in your document root.

  7. Create a new empty file in your text editor.

  8. Inside of the file, place the following CSS:

    .contactMethod {
            font-style: italic;
            font-weight: bold;
    }
  9. Save the file as contact.css in your document root.

  10. Create a new empty file in your editor.

  11. Within the file, place the following code and HTML:

    <?php
     
    require_once("classPage.php");
     
    $page = new Page();
     
    $page->type = "contact";
    $page->titleExtra = "Contact Me";
     
    print $page->getTop();
     
    print <<<EOF
     
    <div id="mainContent"> 
    <h1>Contacting me is easy</h1>
    <p class="contactMethod">suehring@braingia.com</p>
    <p class="contactMethod">Twitter:  @stevesuehring</p>
     
    </div> <!-- end main content →
     
    EOF;
     
    print $page->getBottom();
     
     
    ?>
  12. Save the file as contact.php in your document root.

  13. View the file in your browser

    image0.jpg
  14. Click through each link: Home, About, and Contact Me.

    The pages should work and link to each other.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.