Location: PHPKode > scripts > B-Forms > b-forms/editperson.php
<?php
// Include my local files
require_once("local.inc");
require_once("common.inc");

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

init_db(TRUE); // Initialize the database link in the global $blog_link variable

// Upload config parameters (ideally should be located in a separate config file)
$file_tmp_dir = "/apache/htdocs/examples/pics/tmp";
$file_final_dir = "/apache/htdocs/examples/pics";
$file_tmp_base = "/examples/pics/tmp";
$file_final_base = "/examples/pics";


// Prepare the form
$form = new Form("denied.html");
$bl = & new BaseLayout();


$block = & new Block("person");
$block->add_property(new TextProperty("first_name", "First Name", "", TRUE, 32));
$block->add_property(new TextProperty("last_name", "Last Name", "", TRUE, 32));
$block->add_property(new FileProperty("picture_file", "Picture", FALSE,
                                      $file_tmp_dir, $file_final_dir, TRUE,
                                      new ImageNameDisplay($file_final_base, $file_tmp_base, "")),
                     new FileUpload());
$block->add_property(new LayoutElement("Access rights"), new SectionHeader(&$bl));
$block->add_property(new CheckBoxProperty("admin_fl", "Administrator", FALSE));
$block->add_property(new CheckBoxProperty("manager_fl", "Manager", FALSE));

$block -> add_property(new ButtonProperty("save", "Save", TRUE));
$block -> add_property(new ButtonProperty("cancel", "Cancel"));
$form -> add_block($block);


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

   if (isset($HTTP_GET_VARS["person"])) {

      $query = sprintf(
        "SELECT first_name, last_name, picture_file ".
        "FROM persons WHERE id=%d", $HTTP_GET_VARS["person"]);

      $result = mysql_query($query, $blog_link);
      $num_rows = mysql_num_rows($result);

      if ($num_rows==0) {
         header("Location: ".$form->_denied_target);
         mysql_close($link);
         exit;
      }

      $row = mysql_fetch_row($result);

      $form->person->append(RS_OLD);
      $form->person->id = $HTTP_GET_VARS["person"];
      $form->person->first_name = $row[0];
      $form->person->last_name = $row[1];
      $form->person->_properties["picture_file"]->init($row[2]);

   }
}

function person_cancel_on_action($rownum = -1) {
   close_db();
   header("Location: /examples/");
   exit;
}

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

   if (!$form->validate()) return;

   if ($form->person->get_record_status() == RS_OLD) {

      $query = sprintf(
         "UPDATE persons ".
         "SET    first_name = '%s', ".
         "       last_name = '%s', ".
         "       picture_file = '%s' ".
         "WHERE  id = %d",
         mysql_escape_string($form->person->first_name),
         mysql_escape_string($form->person->last_name),
         mysql_escape_string($form->person->picture_file),
         $form->person->id);

      mysql_query($query, $blog_link);

      $form->person->_properties["picture_file"]->save_file($form->person->id, $form->person->id);

   }
   else {
      // New person
      $query = sprintf(
         "INSERT INTO persons (first_name, last_name, picture_file) VALUES ('%s', '%s', '%s')",
         mysql_escape_string($form->person->first_name),
         mysql_escape_string($form->person->last_name),
         mysql_escape_string($form->person->picture_file));

      mysql_query($query, $blog_link);

      $form->person->id = mysql_insert_id($blog_link);

      $form->person->_properties["picture_file"]->save_file("", $form->person->id);
   }

   close_db();

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

}

function form_pre_display() {
   global $form;
   if ($form->person->id < 0)
      $form->person->_properties["picture_file"]->visible = FALSE;
}

$form->process();

echo "<html><body><head>\n";
echo "<link rel=\"stylesheet\" media=\"screen, projection\" type=\"text/css\" href=\"layout.css\" />\n";
echo "</head>";

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

$form->start_form();

$bl->show_block("person");

$form->end_form();
echo "</body></html>\n";

close_db();
?>
Return current item: B-Forms