Location: PHPKode > scripts > B-Forms > b-forms/tr.php
// Include the library

$days = array( 0=>"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");

// Define the form structure
$form = new Form("denied.html", TRUE);

// Prepare layouts
$bl = & new BaseLayout();
$tl = & new TableLayout();

// Single row block "report": properties
$block = & new Block("report");
$block -> add_property(new TextProperty("name", "Employee Name",
                                        "", TRUE, 64));
$block -> add_property(new DateProperty("start_date", "Week Start Date",
                                        "", TRUE, TRUE));
// report: a layout placeholder for the records
$block -> add_property(new LayoutElement(),
                       new InlineBlock(&$bl, "record", &$tl));
// report: the actions
$block -> add_property(new ButtonProperty("add", "Add Record"));
$block -> add_property(new ButtonProperty("save", "Save", TRUE));
$block -> add_property(new ButtonProperty("submit", "Submit"));
$block -> add_property(new ButtonProperty("cancel", "Cancel"));
$form -> add_block($block);

//Multi-row block "record", for holding project records
$block = & new Block("record", TRUE);
$block -> add_property(new TextProperty("project_name", "Project Name", "", TRUE, 64), new TextBox(30));
$block -> add_property(new ButtonProperty("delete", "Delete"));
for ($i=0; $i<7; $i++)
  $block -> add_property(new NumericProperty("hours_$i", $days[$i], "", FALSE, 2, 1));

$form -> add_block($block);

// This trigger loads the time report itself into the $form->report
// block. A real implementation should read it from the database,
// but we will just hardcode values for simplicity.// Define triggers

function report_on_open() {
   global $form;

      // RS_OLD means that the block will contain
      // an existing record, rather than a new one.

   $form->report->name = "John Smith";
   $form->report->start_date = "2004-09-06";


// This trigger loads the project records that are part of the
// time report loaded in the report_on_open() trigger.

function record_on_open() {
   global $form;

   // For the purpose of this example, we will generate
   // dummy records. In reality, this code should retreive
   // the actual values for the time report, that is
   // contained in $form->report block.

   for ($j=0; $j<6; $j++) {
      $form->record->project_name[$j] = "Project ".($j+1);
      $form->record->id[$j] = $j+1;
      // And we will skip the hours for now

// Cancelling the form is also simple (like it should be) -
// just redirect the browser and exit.

function report_cancel_on_action($rownum = -1) {

   header("Location: /examples/");

// Here we just call the standard $form->save() function that
// will manage the validation and saving process. If it returns
// TRUE, then the save operation has been successful, and we can
// safely redirect the browser.

function report_save_on_action($rownum = -1) {
   global $form;
   // Save the data with status DRAFT

   if ($form->save()) {
      header("Location: /examples/");

// In this trigger we set a global variable $submitted to TRUE
// to signify that when the report will be saved it should be
// saved in status submitted. (We could have had a hidden field
// with status and set the status directly to that field instead).
// In any case we mark the report block as changed and start
// the save sequence.

function report_submit_on_action($rownum = -1) {
   global $form, $submitted;

   // Save the data with status SUBMITTED
   $submitted = TRUE;

   // Ensure that the saving sequence treats the report record
   // as changed

   if ($form->save()) {
      header("Location: /examples/");

// This trigger is called by pressing the "Add Record" button.
// It simply adds a new record to block $form->record and returns.
// Returning from a trigger causes the form to be redisplayed.

function report_add_on_action($rownum = -1) {
   global $form;


// This trigger is called by pressing the "Delete" button next to the
// project field. It deletes the record, where the button was pressed.
// However, the actual deletion only happens when the form is saved, thus
// here we only mark the record for deletion.
// Returning from a trigger causes the form to be redisplayed, but the
// marked record will not be displayed.

function record_delete_on_action($rownum) {
   global $form;


// Saving triggers

function report_on_update($rownum) {
   global $form, $submitted;
   echo "updating report, submitted=$submitted<br>";

function report_on_insert($rownum) {
   global $form;
   echo "inserting report, submitted=$submitted<br>";

function record_on_insert($rownum) {
   global $form;
   echo "inserting record $rownum, project=".$form->record->project_name[$rownum]."<br>";

function record_on_delete($rownum) {
   global $form;
   echo "deleting record $rownum, id=".$form->record->id[$rownum]."<br>";

function record_on_update($rownum) {
   global $form;
   echo "updating record $rownum, project=".$form->record->project_name[$rownum].
        ", id=".$form->record->id[$rownum]."<br>";

// The key method of the library: it does most of the work!


// Generate the HTML code for the form
// Use the layout.css file provided with the library to achieve
// the needed look of the form

<title>Weekly Time Report</title>
<link rel="stylesheet" media="screen, projection"
      type="text/css" href="screen.css" />
<link rel="stylesheet" media="screen, projection"
      type="text/css" href="layout.css" />
<h1>Weekly Time Report</h1>

// This following line will display the error message, if
// an error was found during form validation. This can only
// happen after the form is submitted.

if (isset($error)) echo "<h2>$error</h2>\n";

// Another important call - it will open the form tag and do
// some other preparations.


// And finally, generate the form with just one call!


// A very important call: $form->end_form().
// It generates all the hidden fields needed for the form to work
// and the electronic signature.


echo "</body></html>\n";
Return current item: B-Forms