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

Using a User ID To Store and Retrieve Your Form Data

(3497 views. Last Updated: 2007-08-10 12:51 AM)

Here is an example of how to associate your form data with a 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 myform.php. If the user has already submitted form data, their form data is loaded into the form. After completing myform.php, if form data was previously created, it is updated, otherwise it is created. All form data is associated with a user_id.

1. Use FORMgen to create a new IntegerField (doesn't matter where in the form) with Name: user_id, Label: User ID, Required: Required, Max Length: 10, Min Length: 1, Max Value: 9999999, Min Value: 1. If you are using FORMgen Lite, you will then need to run the create_my_table.php script created on Step 6.
2. This step is not required and is only for advanced users who wish to speed up database performance: use phpMyAdmin to create an index on user_id.
3. Edit your form and replace:
$ffForm->addField(new IntegerField("user_id", "User ID", FORM_FIELD_REQUIRED, 10, 1, null, 9999999, 1));
with:
//$ffForm->addField(new IntegerField("user_id", "User ID", FORM_FIELD_REQUIRED, 10, 1, null, 9999999, 1));
4. Edit your form and replace:
$ffForm->formFields["ff_inserted_on"]->getCurrentDateTime();
$ffForm->insertValuesIntoDb(FF_FORM_NAME);
with:
$ffForm->formFields["ff_inserted_on"]->getCurrentDateTime();

// If the form data exists, update it, otherwise insert the data
if (!isset($_SESSION))
    session_start();
$ffDb = &FfDb::s();
if ($ffDb->getSimpleValue("SELECT user_id FROM " . FF_FORM_NAME . " WHERE user_id=" . $_SESSION["user_id"])) {
    $ffForm->updateValuesInDb(FF_FORM_NAME, "WHERE user_id=" . $_SESSION["user_id"]);
} else {
    $ffForm->addField(new FormField("user_id", null));
    $ffForm->setValue("user_id", $_SESSION["user_id"]);
    $ffForm->insertValuesIntoDb(FF_FORM_NAME);

}
5. Edit your form 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"])) {
    $ffForm->loadValuesFromDb(FF_FORM_NAME, "WHERE user_id=" . $_SESSION["user_id"]);
}
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: myform.php");

?>
Info
Support | Earn Money
Copyright © 2005-2019 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.