<?php

require("xmake.org/php/gk_formmail/gk_formmail.php");

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?=FormScript()?></title>

<style type="text/css">

*.FormResultSuccess {color: green;}
*.FormResultError {color: red;}

</style>

</head>
<body>
<p>This form demonstrates the 'reflexive' use of <a href="<?=dirname($_SERVER['PHP_SELF'])?>">gk_formmail.php</a>. 
In 'reflexive' mode, a single php form does all the work: 
accepts user input; validates the input; sends email containing data collected; displays error or success messages to the user. 
This is recommended over 'static' mode, which requires multiple html pages to do the same work, since: 1) fewer pages need to be maintained, 2) though equally secure as reflexive mode, in most respects, users can view the source of html pages but cannot view the source of php pages, which ensures that you will never accidentally expose private information in your forms. 

<p>gk_formmail.php requires that each form has an associated configuration file, named in relation to the form file.
In this case, the form file is '<a href="<?=$_SERVER['PHP_SELF']?>.source.php"><?=basename($_SERVER['PHP_SELF'])?></a>' so the related config file must be named '<a href="<?=$_SERVER['PHP_SELF']?>.config.php.source.php"><?=basename($_SERVER['PHP_SELF'])?>.config.php</a>.'

<p>We've included sample fields to show you how to
submit Checkboxes, Radio Buttons, Multi-selection
List Boxes, normail Text fields, and Text Areas.

<p>Actual sending of email has been disabled in the demo form below. Instead, we simply display the collected data from the form (with minimal formatting) that would normally be sent by email.

<p>

-- Form Results: <br />
<?=FormResult()?>
<?=FormErrorList
()?>
<?=FormResults
()?>
<hr>

<p>
<form method="post" action="<?=FormScript()?>" name="SampleForm">

    <table border="1" cellspacing="5%">
    <tr>
        <td>
        <span class='FormResultError'><?=FormError('realname')?></span>

        <p>Please enter your name:</p>
        </td>
        <td><input type="text" name="realname" value="<?=FormValue('realname')?>"/>
        </td>
    </tr>
    <tr>
        <td>
        <span class='FormResultError'><?=FormError('email')?></span>
        <p>Please enter your email address:</p>
        </td>
        <td><input type="text" name="email" value="<?=FormValue('email')?>"/>
        </td>
    </tr>
    <tr>
        <td>
        <span class='FormResultError'><?=FormError('contact')?></span>
        <p>May we contact you?</p>
        </td>
        <td>
            Yes <input type="radio" name="contact" value="Y" <?=FormValueChecked('contact''Y'TRUE)?> />
            No <input type="radio" name="contact" value="N" <?=FormValueChecked('contact''N')?>/>
         </td>
    </tr>
    <tr>
        <td>
        <span class='FormResultError'><?=FormError('colors')?></span>
        <p>What are your favourite colours?</p>
        </td>
        <td>
            Red     <input type="checkbox" name="colors[]" value="red" <?=FormValueChecked('colors''red')?>/>
            Blue    <input type="checkbox" name="colors[]" value="blue" <?=FormValueChecked('colors''blue')?>/>
            Yellow  <input type="checkbox" name="colors[]" value="yellow" <?=FormValueChecked('colors''yellow')?>/>
         </td>
    </tr>
    <tr>
        <td valign="top"><p>What vehicles do you have a license to operate?</p>
        </td>
        <td valign="top">
        <span class='FormResultError'><?=FormError('vehicles')?></span>
            <select name="vehicles[]" multiple size="5">
                <option value="Car" <?=FormValueSelected('vehicles''Car')?>>Car</option>
                <option value="Bus" <?=FormValueSelected('vehicles''Bus')?>>Bus</option>
                <option value="Truck" <?=FormValueSelected('vehicles''Truck')?>>Truck</option>
                <option value="Plane" <?=FormValueSelected('vehicles''Plane')?>>Aeroplane</option>
                <option value="Boat" <?=FormValueSelected('vehicles''Boat')?>>Boat</option>
            </select>&nbsp;(CTRL-click to select multiple items)
         </td>
    </tr>
    <tr>
        <td valign="top">
        <span class='FormResultError'><?=FormError('mesg')?></span>
        <p>Please enter your message:</p>
        </td>
        <td><textarea name="mesg" rows="3" cols="50"><?=FormValue('mesg')?></textarea>
        </td>
    </tr>
    </table>
   <input type="submit" value="Submit" name="submit"/>
</form>
<p>

<hr>
<h3>Brief Analysis - detailed documentation of script operation is included in the comments inside gk_formmail.php master controlling script.
</h3>

This demo shows how to:
<br />

1. maintain the state of form inputs
<br />
2. handle errors interactively
<br />
3. display a 'success' message when done

<p>
Note that there is no need for redirection to separate error / success pages (though optional use of tectite 'good_url' and 'bad_url' redirection still works). 
<p>
This file initially includes the gk_formmail.php script, which processes form input and sets some global variables that can then be used within your form page to customize the display of messages to the user.
<p>
This is all done without the need for any PHP logic embedded in the form HTML, thus satisfying one of the cardinal rules of good design: separation of logic from formatting!
<p>
This is possible by simply echoing the values of functions provided by gk_formmail.php, whose output is determined dynamically.
<p>
For example, echoing the output of a function such as FormResult() will display the correct output to the user, depending on the current state of the form:
<br />
* Nothing - the first time the page is loaded.
<br />
* An error message - if the form inputs fail any validation tests.
<br />
* A success message - if all form inputs passed the validation tests and submission was successful.

<p>
There are three files required (click links to view source):
<br />
+ the HTML form: <a href="<?=$_SERVER['PHP_SELF']?>.source.php"><?=basename($_SERVER['PHP_SELF'])?></a>
<br />
+ a config file: <a href="<?=$_SERVER['PHP_SELF']?>.config.php.source.php"><?=basename($_SERVER['PHP_SELF'])?>.config.php</a>
<br />
+ gk_formmail.php (available for licensing): the master 'controlling' script.
<p>
Each form has an associated configuration file, named in relation to the 'action' handler for the form: [action].config.php
<br />
In this case, the form is its own 'action' so the name of the config file is directly related to this file: <a href="<?=$_SERVER['PHP_SELF']?>.config.php.source.php"><?=basename($_SERVER['PHP_SELF'])?>.config.php</a>
<br />
Alternately, you could allow multiple pages to share the same form 'action' file, and the same configuration file, without the need for redundant configuration within the several HTML forms (as would be required by tectite formmail).
<p>
Below is some debugging output that reflects the current state of some of the PHP variables available to this form.
<br />
The default values of FormErrorText and FormSuccessText were configured in the associated config file.
<p>
<hr>

<?php
echo ("FormResultText()=".FormResultText()."<p>");
echo (
"\$OUTPUT_VALUES['FormErrorText']=".$OUTPUT_VALUES['FormErrorText']."<p>");
echo (
"\$OUTPUT_VALUES['FormErrorItemList']=".$OUTPUT_VALUES['FormErrorItemList']."<p>");
echo (
"\$OUTPUT_VALUES['FormSuccessText']=".$OUTPUT_VALUES['FormSuccessText']."<p>");
echo (
"\$OUTPUT_VALUES['FormResults']=".$OUTPUT_VALUES['FormResults']."<p>");
echo (
"\$OUTPUT_VALUES=<p>");
print_r($OUTPUT_VALUES);
?>
<hr>
This is a modified version of tectite formmail and many requirements have been changed.
<br />
For more information, see comments inside gk_formmail.php
<br />
For more information about tectite formmail, visit
<a href="http://www.tectite.com/">www.tectite.com</a>
</body>
</html>
1