<?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('dependent-fields') === true) {
    // create validator & auto-validate required fields
    $validator = Form::validate('dependent-fields');

    // additional validation

	
    // check for errors
    if ($validator->hasErrors()) {
        $_SESSION['errors']['dependent-fields'] = $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();
        $insert['IPO_Submitter'] = Mysql::SQLValue($_POST['IPO_Submitter']);
        $insert['IPO_Manufacturer'] = Mysql::SQLValue($_POST['IPO_Manufacturer']);
        $insert['IPO_MPN'] = Mysql::SQLValue($_POST['IPO_MPN']);
        $insert['IPO_Desc'] = Mysql::SQLValue($_POST['IPO_Desc']);
        $insert['IPO_Type'] = Mysql::SQLValue($_POST['IPO_Type']);
        if (!$db->insertRow('IPO', $insert)) {
            $user_message = '<p class="alert alert-danger">' . $db->error() . '<br>' . $db->getLastSql() . '</p>' . "\n";
        } else {
            $user_message = '<p class="alert alert-success">Thanks for subscribe !</p>' . "\n";
            Form::clear('dependent-fields');
        }
        Form::clear('dependent-fields'); // just for demo ; delete this line if real database recording.
        $user_message = '<p class="alert alert-success">Thanks for signing up !</p>' . "\n"; // just for demo ; delete this line if real database recording.
    }
}

/* ==================================================
    The Form
================================================== */

$form = new Form('dependent-fields', 'horizontal', 'novalidate');

/* Orderer */

$form->startFieldset('First Thing\'s First.  Who\'s Ordering?');
$form->addOption('IPO_Submitter', '', 'Your Name');
$form->addOption('IPO_Submitter', 'Jeff', 'Jeff', '', '');
$form->addOption('IPO_Submitter', 'Jim', 'Jim', '', '');
$form->addOption('IPO_Submitter', 'Joe', 'Joe', '', '');
$form->addOption('IPO_Submitter', 'Julie', 'Julie', '', '');
$form->addOption('IPO_Submitter', 'Mel', 'Mel', '', '');
$form->addOption('IPO_Submitter', 'Rick', 'Rick', '', '');
$form->addSelect('IPO_Submitter', 'Ordering?', 'class=selectpicker, required');



/* Product */


$form->startFieldset('What Do You Need?');


$form->setCols(0, 6);
$form->groupInputs('IPO_Manufacturer', 'IPO_MPN', 'IPO_Desc');
$form->addInput('text', 'IPO_Manufacturer', '', '', 'required=required, placeholder=Manufacturer?');
$form->addInput('text', 'IPO_MPN', '', '', 'required=required, placeholder=Manufacturer Part Number');
$form->addInput('text', 'IPO_Desc', '', '', 'required=required, placeholder=Description');

$form->setCols(0, 0);
$form->addCheckbox('IPO_Type', 'stock', 'Stock');
$form->addCheckbox('IPO_Type', 'customer', 'Customer');
$form->printCheckboxGroup('IPO_Type', 'Ordering For', false);


$form->setCols(3, 9, 'sm');
$form->addBtn('reset', 'reset-btn', 1, 'Reset <span class="glyphicon glyphicon-cancel append"></span>', 'class=btn btn-warning', 'my-btn-group');
$form->addBtn('submit', 'submit-btn', 1, 'Send <span class="glyphicon glyphicon-envelope append"></span>', 'class=btn btn-success ladda-button, data-style=zoom-in', 'my-btn-group');
$form->printBtnGroup('my-btn-group');

// Custom radio & checkbox css
$form->addPlugin('nice-check', 'form', 'default', ['%skin%' => 'purple']);

// jQuery validation
$form->addPlugin('formvalidation', '#dependent-fields');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Special Offer Sign Up Form - Php Form Builder</title>
    <meta name="description" content="Bootstrap Form Generator - how to create a Special Offer Sign Up with Php Form Builder Class">
    <link rel="canonical" href="https://www.phpformbuilder.pro/templates/bootstrap-3-forms/dependent-fields.php" />
    <!-- Change the link to bootstrap.min.css according to your directory structure -->
    <link rel="preload" href="../assets/css/bootstrap.min.css" as="style" onload="this.rel='stylesheet'">
    <?php $form->printIncludes('css'); ?>
    <style type="text/css">
        .help-block {
            padding-left: 0;
        }
    </style>
    <?php $form->printIncludes('css'); ?>
</head>
<body>
    <h1 class="text-center">Php Form Builder - Special Offer Sign Up Form<br><small>vertical form with helpers</small></h1>
    <div class="container">
        <div class="row">
            <div class="col-sm-10 col-sm-offset-1 col-md-6 col-md-offset-3">
            <?php
            if (isset($user_message)) {
                echo $user_message;
            }
            $form->render();
            ?>
            </div>
        </div>
    </div>

    <!-- jQuery -->

    <script src="//code.jquery.com/jquery.min.js"></script>

    <!-- Bootstrap JavaScript -->

    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <?php
        $form->printIncludes('js');
        $form->printJsCode();
    ?>
</body>
</html>

