<?php
require_once("includes/redirect.php");
include_once("includes/formlib.php");

$title = "FormLib Test Page";
main($title);

function main($title = "") {
    $f = new FormLib("error", HORIZONTAL);
    if (isset($_REQUEST["submitTest"])) {
        checkForm($f);
        if (!$f->isError()) { // data is OK
            processData($f);
            redirect("echo.php?it=worked");
        }
    }
    require("includes/header.php");
    showContent($title, $f);
    require("includes/footer.php");
}

// Check form for errors and return error messages
function checkForm(&$f) {
    $f->isEmpty('name', "You must enter a name");
    $f->isEmpty('state', "You must select a state");
    $f->isEmpty('lang', "You must select a language");
}

// Save the data
function processData($f) {
    // Save the data;
    $name = $f->getValue("name");
    $sex = $f->getValue("sex");
    $state = $f->getValue("state");
    $lang = $f->getValue("lang");
    $comments = $f->getValue("comments");

    include_once("includes/dbconvars.php");
    @$dbCnx = mysql_connect($dbhost, $dbuser, $dbpwd)
        or die("Could not connect");
    mysql_select_db($dbname, $dbCnx)
        or die(mysql_error());
    $sql = "
      INSERT INTO someTable(Name, Sex, State, Language, Comments )
      VALUES ('$name', '$sex', '$state[0]', '$lang[0]', '$comments')
      ";

    // For debugging (remove when finished)
    echo "<pre>\n";
    print_r($_REQUEST);
    echo "</pre>\n";
    echo "<p>SQL: $sql</p>\n";
    die("Stopped for testing"); // stop the redirect

    mysql_query($sql) or die(mysql_error());
    mysql_close($dbCnx);
}

// Display the content of a page
function showContent($title, $f) {
    echo "<h1>$title</h1>\n";
    echo $f->reportErrors();
    echo $f->start();
    echo "<table>\n<tr>\n";
    echo "<th align=\"left\">Description</th>\n";
    echo "<th align=\"left\">Entry</th>\n";
    echo "</tr>\n<tr>\n<td>";
    echo $f->formatOnError('name', 'Name:');
    echo "</td>\n<td>";
    echo $f->makeTextInput('name', 15);
    echo $f->showMessageOnError('name');
    echo "</td>\n</tr>\n<tr>\n<td>";
    echo $f->formatOnError('sex', 'Sex:');
    echo "</td>\n<td>";
    $list = array("Male"=>"m", "Female"=>"f", "Not specified"=>"n");
    echo $f->makeRadioGroup('sex', $list, "n");
    echo "</td>\n</tr>\n<tr>\n<td>";
    echo $f->formatOnError('state', 'State:');
    echo "</td>\n<td>";
    $list = array("- Select One -"=>"", "California"=>"CA",
        "Oregon"=>"OR", "Washington"=>"WA");
    $selectList = array("CA", "WA");
    echo $f->makeSelectMulti('state', $list, $selectList);
    echo $f->showMessageOnError('state');
    echo "</td>\n</tr>\n<tr>\n<td>";
    echo $f->formatOnError('lang', 'Languages:');
    echo "</td>\n<td>\n";
    $labelValueList = array("ASP"=>"a", "JSP"=>"j", "PHP"=>"p",
        "Other"=>"o");
    $selectList = array("p");
    if (isset($_REQUEST["submitTest"])) $selectList = array();
    echo $f->makeCheckBoxes('lang', $labelValueList, $selectList);
    echo $f->showMessageOnError('lang');
    echo "</td>\n</tr>\n<tr>\n<td>";
    echo $f->formatOnError('sfill','Comments (Optional):');
    echo "</td>\n<td>";
    echo $f->makeTextArea('comments', 5, 45,
        "Enter comments here");
    echo "</td>\n</tr>\n<tr>\n<td>";
    echo $f->makeButton("Save");
    echo "</td>\n</tr>\n</table>\n";
    echo $f->finish();
}
?>