Login   Register  
PHP Classes
elePHPant
Icontem

File: example2.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jeff Williams  >  Easy Template  >  example2.php  >  Download  
File: example2.php
Role: Example script
Content type: text/plain
Description: Example 2
Class: Easy Template
Template engine based on real HTML tag replacement
Author: By
Last change:
Date: 2007-03-12 11:10
Size: 3,025 bytes
 

Contents

Class file image Download
<?php
include("template.class.php");

// If the form has not been posted...
if (! isset($_POST["Submit"])) {

    
// Show the data entry form
    
showForm();

} else { 
// We have posted data

    // Let's check the POST (or GET) data and validate
    
$problem validateForm();

    
// If there were any problems then show them
    
if (strlen($problem) > 0) {

        
// Show the main registration form and pass in the validation problems
        
showForm('<h3 style="color: red;">' $problem '</h3>');

    } else { 
// Data Validation Passed

        // SAVE THE RECORD OR PROCESS YOUR DATA HERE
        // Let's show a confirmation (which could be another template)
        
echo "<h2>Your data was submitted successfully</h2>\n";

        
//Now show the passed in values using the Template::getFormValue value
        //NOTE: we could also show these values using $_POST[] if needed
        
echo "First Name: " Template::getFormValue("FirstName") . "<br />\n";
        echo 
"Zip Code: " Template::getFormValue("ZipCode") . "<br />\n";
        echo 
"Gender: " Template::getFormValue("Gender") . "<br />\n";
        echo 
"Agree: " . (Template::getFormValue("Agree") == "on" "Yes" "No");
    }
}
// --------------------------------------------------------------------------

//  Show the input form
function showForm($message="") {

    
// Create the template object
    
$template = new Template("example2.html");

    
$template->replaceTag("Title""My Template Example");
    
$template->replaceTag("Message"$message);
    
$template->replaceTag("FirstName"Template::getFormValue("FirstName"));
    
$template->replaceTag("ZipCode"Template::getFormValue("ZipCode"));
    
$template->replaceTag("Agree"Template::getHTMLChecked(Template::getFormValue("Agree")));

    
// Populate the radio buttons group
    
if (Template::getFormValue("Gender") == "F") {
        
$template->replaceTag("GenderMale"Template::getHTMLChecked(false));
        
$template->replaceTag("GenderFemale",  Template::getHTMLChecked(true));
    } else {
        
$template->replaceTag("GenderMale"Template::getHTMLChecked(true));
        
$template->replaceTag("GenderFemale"Template::getHTMLChecked(false));
    }

    
$template->showPage();
}

//  Validate (server side for security) the values from the form
function validateForm() {

    
// Validate the form values
    
$problem "";

    if (
strlen(trim(Template::getFormValue("FirstName"))) == 0) {
        
$problem .= "First name is required<br />";
    } elseif (
strlen(trim(Template::getFormValue("FirstName"))) > 20) {
        
$problem .= "First name is too long<br />";
    }

    if (! 
is_numeric(Template::getFormValue("ZipCode"))) {
        
$problem .= "Zip code must be a valid number<br />";
    } elseif (
strlen(trim(Template::getFormValue("ZipCode"))) <> 5) {
        
$problem .= "Zip code must be 5 digits long<br />";
    }

    
// Pass back any problems
    
return $problem;

}
?>