<?php
ob_start(); // for redirect

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) {
    // Code verification tests here
    $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', '$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();
?>
<p><table>
<tr>
  <th align="left">Description</th>
  <th align="left">Entry</th>
</tr>
<tr>
  <td><?php echo $f->formatOnError('name', 'Name:') ?></td>
  <td><?php echo $f->makeTextInput('name', 15) ?>
  <?php echo $f->showMessageOnError('name') ?></td>
</tr>
<tr>
  <td><?php echo $f->formatOnError('sex', 'Sex:') ?></td>
  <td><?php
    $list = array("Male"=>"m", "Female"=>"f", "Not specified"=>"n");
    echo $f->makeRadioGroup('sex', $list, "n");
  ?></td>
</tr>
<tr>
  <td><?php echo $f->formatOnError('state', 'State:') ?></td>
  <td><?php
    $list = array("- Select One -"=>"", "California"=>"CA",
            "Oregon"=>"OR", "Washington"=>"WA");
    echo $f->makeSelect('state', $list);
    echo $f->showMessageOnError('state');
  ?></td>
</tr>
<tr>
  <td><?php echo $f->formatOnError('lang', 'Languages:') ?></td>
  <td><?php
    $list = array("ASP"=>"a", "JSP"=>"j", "PHP"=>"p", "Other"=>"Other");
    $selectList = array("p");
    if (isset($_REQUEST["submitTest"])) $selectList = array();
    echo $f->makeCheckBoxes('lang', $list, $selectList);
    echo $f->showMessageOnError('lang');
  ?></td>
</tr>
<tr>
  <td>Comments (Optional):</td>
  <td><?php
    echo $f->makeTextArea('comments', 5, 45, "Enter comments here")
  ?></td>
<tr>
  <td><?php echo $f->makeButton("Save") ?></td>
  <td>&nbsp;</td>
</tr>
</table></p>
<?php
    echo $f->finish();
}
?>