PHP Classes

File: edittopic.php

Recommend this page to a friend!
  Classes of alexei peterkin   B-Forms   edittopic.php   Download  
File: edittopic.php
Role: Example script
Content type: text/plain
Description: Example form: edit blog topic
Class: B-Forms
Compose and generate complex web forms
Author: By
Last change: Updated for release 1.2
Date: 19 years ago
Size: 3,273 bytes
 

Contents

Class file image Download
<?
// Here I call some of my local settings and functions
require_once("local.inc");
require_once(
"common.inc");

// Include the library
require_once("b-forms/b-forms.inc");
require_once(
"b-forms/layout.inc");

init_db(TRUE); // This is my local function that opens $blog_link mysql connection.

// Define the form structure

$form = new Form("denied.html");
$bl = new BaseLayout();

$block = & new Block("topic");
$block->add_property(new TextProperty("name", "Name", "", TRUE, 64));
$block -> add_property(new ButtonProperty("save", "Save", TRUE));
$block -> add_property(new ButtonProperty("delete", "Delete"));
$block -> add_property(new ButtonProperty("cancel", "Cancel"));

$form -> add_block($block);

// Define triggers

function form_on_open() {
   global
$form, $blog_link, $HTTP_GET_VARS;

   if (isset(
$HTTP_GET_VARS["topic"])) {
     
$query = sprintf("SELECT name FROM topics WHERE id = %d",$HTTP_GET_VARS["topic"]);
     
$result = mysql_query($query, $blog_link);

     
$num_rows = mysql_num_rows($result);
      if (
$num_rows > 0) {
        
$row = mysql_fetch_row($result);

        
$form->topic->append(RS_OLD);

        
$form->topic->id = $HTTP_GET_VARS["topic"];
        
$form->topic->name = $row[0];
      }
   }
}

function
topic_cancel_on_action($rownum = -1) {
  
close_db();

  
header("Location: /examples/");
   exit;
}

function
topic_save_on_action($rownum = -1) {
    global
$blog_link, $form;

    if (
$form->validate()) {
       if (
$form->topic->get_record_status() == RS_OLD) {
         
// We are saving an existing record

         
$query = sprintf(
            
"UPDATE topics ".
            
"SET name = '%s' ".
            
"WHERE id = %d",
            
mysql_escape_string($form->topic->name),
            
$form->topic->id);

        }
        else {
// We are saving a new record
          
$query = sprintf("INSERT INTO topics (name) VALUES ('%s')",
                           
mysql_escape_string($form->topic->name));
        }

        @
mysql_query($query, $blog_link);

       
// Check if the query executed successfully.
       
if (mysql_errno()) {
          
$error = mysql_error();
           return;
        }

       
close_db();
       
header("Location: /examples/");
        exit;
    }
}

function
topic_delete_on_action($rownum = -1) {
   global
$blog_link, $form;

  
$query = sprintf("DELETE FROM topics WHERE id=%d", $form->topic->id);
  
mysql_query($query, $blog_link);

  
close_db();
  
header("Location: /examples/");
   exit;
}

function
form_pre_display() {
   global
$form;
   if (!
$form->topic->is_record_existing())
     
$form->topic->_properties["delete"]->visible = FALSE;
}

// Do the actual form processing

$form->process();

// Generate the HTML code for the form

echo "<html><body><head>\n";
echo
"<link rel=\"stylesheet\" media=\"screen, projection\" type=\"text/css\" href=\"layout.css\" />\n";
echo
"</head>";
echo
"<h1>".($form->topic->is_record_existing()?"Edit":"Create").
    
" topic</h1>\n";
if (isset(
$error))
   echo
"<h2>$error</h2>";

$form->start_form();
$bl->show_block("topic");
$form->end_form();

echo
"</body></html>\n";

close_db();
?>