<?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'] = 'swilsone@squarespace.com';
$_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();
?>
<fieldset>
<legend>
Update User
</legend>
<div class="form-group row justify-content-end">
<label class="main-label col-sm-4 col-form-label">
Civility :
</label>
<div class="col-sm-8">
<div class="form-check form-check-inline">
<input type="radio" id="civility_0" name="civility" value="M." class="form-check-input">
<label for="civility_0" class="form-check-label">
M.
</label>
</div>
<div class="form-check form-check-inline">
<input type="radio" id="civility_1" name="civility" value="Mrs." class="form-check-input">
<label for="civility_1" class="form-check-label">
Mrs.
</label>
</div>
<div class="form-check form-check-inline">
<input type="radio" id="civility_2" name="civility" value="Ms." checked="checked" class="form-check-input">
<label for="civility_2" class="form-check-label">
Ms.
</label>
</div>
</div>
</div>
<div class="form-group row justify-content-end">
<label for="user-name-8" class="col-sm-4 col-form-label">
Name <sup class="text-danger">* </sup>
</label>
<div class="col-sm-8">
<input id="user-name-8" name="user-name-8" type="text" value="Wilson" size="60" required="required" class="form-control">
</div>
</div>
<div class="form-group row justify-content-end">
<label for="user-first-name-8" class="col-sm-4 col-form-label">
First Name <sup class="text-danger">* </sup>
</label>
<div class="col-sm-8">
<input id="user-first-name-8" name="user-first-name-8" type="text" value="Susan" size="60" required="required" class="form-control">
</div>
</div>
<div class="form-group row justify-content-end">
<label for="user-email-8" class="col-sm-4 col-form-label">
email : <sup class="text-danger">* </sup>
</label>
<div class="col-sm-8">
<input id="user-email-8" name="user-email-8" type="email" value="swilsone@squarespace.com" size="60" required="required" class="form-control">
</div>
</div>
<div class="form-group row justify-content-end">
<label class="main-label col-sm-4 col-form-label">
Validated
</label>
<div class="col-sm-8">
<div class="form-check form-check-inline">
<input type="radio" id="validated_0" name="validated" value="1" checked="checked" class="form-check-input">
<label for="validated_0" class="form-check-label">
Yes
</label>
</div>
<div class="form-check form-check-inline">
<input type="radio" id="validated_1" name="validated" value="0" class="form-check-input">
<label for="validated_1" class="form-check-label">
No
</label>
</div>
</div>
</div>
<div class="form-group row justify-content-end">
<div class=" col-sm-8">
<div class="btn-group">
<button type="button" name="cancel" value="0" class="btn btn-warning">
<span class="fa fa-ban prepend"></span>
Cancel
</button>
<button type="submit" name="submit" value="1" class="btn btn-success">
Submit <span class="fa fa-check append"></span>
</button>
</div>
</div>
</div>
</fieldset>