FORMfields, the premiere web framework
Login
Support Home | Knowledge Base | Documentation | Forums | Contact Us
Info
FORMfields & FORMgen :: Database

Multipart Form With Multiple Databases and User ID

(3199 views. Last Updated: 2007-08-10 7:12 PM)

This example builds on both http://www.formfields.com/support/viewPosting.php?pi=42 and http://www.formfields.com/support/viewPosting.php?pi=45 by creating a multipart form that associates form data in two different database tables with a single User ID. This example uses a session to store the User ID. In the following example, the user visits login.php and then is automatically logged in with a user_id of 7 and then forwarded to form_1.php. If the user has already submitted form data, their form data is loaded into the form. After completing form_1.php, if the form data was previously created, it is updated, otherwise it is created.

Note: We highly recommend that you save (or copy) all forms to /FORMfields/forms/modified and edit them in this directory.

1. Use FORMgen to create a form with Form Name "form_1" and fields:
a. PersonNameField - Name: name, Label: Name, Required: Required
b. EmailAddrField - Name: email, Label: Email, Required: Required
c. IntegerField - Name: user_id, Label: User ID, Required: Required, Max Length: 10, Min Length: 1, Max Value: 9999999, Min Value: 1

2. Use FORMgen to create a form with Form Name "form_2" and fields:
a. CountryField - Name: country, Label: Country, Required: Required
b. IntlPhoneNumberField - Name: phone, Label: Phone, Required: Required
c. IntegerField - Name: user_id, Label: User ID, Required: Required, Max Length: 10, Min Length: 1, Max Value: 9999999, Min Value: 1

3. Edit form_1.php and replace:
$ffForm = new FfForm();
$ffForm->addField(new PersonNameField("name", "Name", FORM_FIELD_REQUIRED));
$ffForm->addField(new EmailAddrField("email", "Email", FORM_FIELD_REQUIRED));
$ffForm->addField(new SubmitField("ff_submit", "Submit"));
$ffForm->addField(new SubmitField("ff_clear", "Clear"));
$ffForm->addField(new SubmitField("ff_cancel", "Cancel"));
with:
$ffForm1 = new FfForm();
$ffForm1->addField(new PersonNameField("name", "Name", FORM_FIELD_REQUIRED));
$ffForm1->addField(new EmailAddrField("email", "Email", FORM_FIELD_REQUIRED));

$ffForm2 = new FfForm();
$ffForm2->addField(new CountryField("country", "Country", FORM_FIELD_REQUIRED, "[Select One]"));
$ffForm2->addField(new IntlPhoneNumberField("phone", "Phone", FORM_FIELD_REQUIRED));

$ffForm = new FfForm();
$ffForm->addFields($ffForm1);
$ffForm->addFields($ffForm2);

$ffForm->addField(new SubmitField("ff_submit", "Submit"));
$ffForm->addField(new SubmitField("ff_clear", "Clear"));
$ffForm->addField(new SubmitField("ff_cancel", "Cancel"));

4. Edit form_1.php and replace:
if (FF_INSERT_INTO_DB) {
    $ffForm->addField(new DateTimeField("ff_inserted_on", "Inserted On", FORM_FIELD_REQUIRED, null));
    $ffForm->formFields["ff_inserted_on"]->getCurrentDateTime();
    $ffForm->insertValuesIntoDb(FF_FORM_NAME);
}
with:
if (FF_INSERT_INTO_DB) {

    // If the form data exists, update it, otherwise insert the data

    if (!isset($_SESSION))
        session_start();

    $ffDb = &FfDb::s();
    global $ffForm1;
    global $ffForm2;

    $ffForm1->getParameters();
    $ffForm2->getParameters();

    if ($ffDb->getSimpleValue("SELECT user_id FROM " . FF_FORM_NAME . " WHERE user_id=" . $_SESSION["user_id"])) {

        $ffForm1->updateValuesInDb(FF_FORM_NAME, "WHERE user_id=" . $_SESSION["user_id"]);
        $ffForm2->updateValuesInDb("form_2", "WHERE user_id=" . $_SESSION["user_id"]);

    } else {

        $ffForm1->addField(new DateTimeField("ff_inserted_on", "Inserted On", FORM_FIELD_REQUIRED, null));
        $ffForm1->formFields["ff_inserted_on"]->getCurrentDateTime();
        $ffForm1->addField(new FormField("user_id", null));
        $ffForm1->setValue("user_id", $_SESSION["user_id"]);
        $ffForm1->insertValuesIntoDb(FF_FORM_NAME);

        $ffForm2->addField(new DateTimeField("ff_inserted_on", "Inserted On", FORM_FIELD_REQUIRED, null));
        $ffForm2->formFields["ff_inserted_on"]->getCurrentDateTime();
        $ffForm2->addField(new FormField("user_id", null));
        $ffForm2->setValue("user_id", $_SESSION["user_id"]);
        $ffForm2->insertValuesIntoDb("form_2");

    }

}

5. Edit form_1.php and replace:
$ffForm->loadDbValues($GLOBALS["FF_DEFAULTS"]);
// Enter other default values here. Example: 
//$ffForm->setValue("name", "John");
with:
$ffForm->loadDbValues($GLOBALS["FF_DEFAULTS"]);
// Enter other default values here. Example: 
//$ffForm->setValue("name", "John");

// Load the form data if it exists
if (!isset($_SESSION))
    session_start();
$ffDb = &FfDb::s();
if ($ffDb->getSimpleValue("SELECT user_id FROM " . FF_FORM_NAME . " WHERE user_id=" . $_SESSION["user_id"])) {

    // Load the values from the 1st part of the form and copy them to the combined form
    global $ffForm1;
    $ffForm1->loadValuesFromDb(FF_FORM_NAME, "WHERE user_id=" . $_SESSION["user_id"]);
    $ffForm->copyValues($ffForm1);

    // Load the values from the 2nd part of the form and copy them to the combined form
    global $ffForm2;
    $ffForm2->loadValuesFromDb("form_2", "WHERE user_id=" . $_SESSION["user_id"]);
    $ffForm->copyValues($ffForm2);
}

6. Create or modify your login page to populate $_SESSION["user_id"]. Here is an dummy login page that you can use for testing:
login.php:
<?php

    
if (!isset($_SESSION))
        
session_start();

    
$_SESSION["user_id"] = 7;
    
    
header("Location: form_1.php");

?>

For details on how to modify your Control Panel to work with this example, please see http://www.formfields.com/support/viewPosting.php?pi=52
Info
Support | Earn Money
Copyright © 2005-2018 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.