<?php
use phpformbuilder\Form;
use phpformbuilder\Validator\Validator;
use phpformbuilder\database\Mysql;
/* =============================================
start session and include form class
============================================= */
session_start();
include_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . '/phpformbuilder/Form.php';
/* =============================================
validation if posted
============================================= */
if ($_SERVER["REQUEST_METHOD"] == "POST" && Form::testToken('user-form') === true) {
include_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . '/phpformbuilder/Validator/Validator.php';
include_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . '/phpformbuilder/Validator/Exception.php';
$validator = new Validator($_POST);
$required = array('civility', 'user-name-8', 'user-first-name-8', 'user-email-8', 'validated');
foreach ($required as $required) {
$validator->required()->validate($required);
}
$validator->email()->validate('user-email-8');
// check for errors
if ($validator->hasErrors()) {
$_SESSION['errors']['user-form'] = $validator->getAllErrors();
} else {
require_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . 'phpformbuilder/database/db-connect.php';
require_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . 'phpformbuilder/database/Mysql.php';
$db = new Mysql();
$filter['user_id'] = Mysql::sqlValue($_POST['user_id'], Mysql::SQLVALUE_NUMBER);
$update['civility'] = Mysql::SQLValue($_POST['civility']);
$update['user-name-8'] = Mysql::SQLValue($_POST['user-name-8']);
$update['user-first-name-8'] = Mysql::SQLValue($_POST['user-first-name-8']);
$update['user-email-8'] = Mysql::SQLValue($_POST['user-email-8']);
$update['validated'] = Mysql::SQLValue($_POST['validated']);
/* (disabled in demo - no database enabled)
if (!$db->UpdateRows('users', $update, $filter)) {
$msg = '' . $db->error() . '
' . $db->getLastSql() . '
' . " \n";
} else {
$msg = 'Database updated successfully !
' . " \n";
}
*/
}
}
if(isset($_GET['user_id']) && is_numeric($_GET['user_id'])) {
$user_id = $_GET['user_id'];
}
if (!isset($_SESSION['errors']['user-form']) || empty($_SESSION['errors']['user-form'])) { // If no error posted
/* Retrieve values from db (disabled in demo - no database enabled)
$db = new Mysql();
$columns = $db->getColumnNames("users");
$qry = "SELECT * FROM users WHERE user_id='$user_id'";
$db->query($qry);
$row = $db->Row();
foreach ($columns as $columnName) {
$_SESSION['user-form'][$columnName] = $row->$columnName;
}
*/
// values for demo
$user_id = 1;
$_SESSION['user-form']['civility'] = 'Ms.';
$_SESSION['user-form']['user-name-8'] = 'Wilson';
$_SESSION['user-form']['user-first-name-8'] = 'Susan';
$_SESSION['user-form']['user-email-8'] = '[email protected]';
$_SESSION['user-form']['validated'] = 1;
}
$form = new form('user-form');
$form->startFieldset('Update User');
$form->addInput('hidden', 'user_id', $user_id);
$form->addRadio('civility', 'M.', 'M.');
$form->addRadio('civility', 'Mrs.', 'Mrs.');
$form->addRadio('civility', 'Ms.', 'Ms.');
$form->printRadioGroup('civility', 'Civility : ');
$form->addInput('text', 'user-name-8', '', 'Name', 'size=60, required=required');
$form->addInput('text', 'user-first-name-8', '', 'First Name', 'size=60, required=required');
$form->addInput('user-email', 'user-email-8', '', 'user-email : ', 'size=60, required=required');
$form->addRadio('validated', 'Yes', 1);
$form->addRadio('validated', 'No', 0);
$form->printRadioGroup('validated', 'Validated');
$form->addBtn('button', 'cancel', 0, 'Cancel', 'class=btn btn-warning', 'btn-group');
$form->addBtn('submit', 'submit', 1, 'Submit ', 'class=btn btn-success', 'btn-group');
$form->printBtnGroup('btn-group');
$form->endFieldset();
?>