示例#1
0
<?php

require_once '../../src/autoload.php';
require_once '../../vendor/autoload.php';
$form = new \X2Form\Form('ContactForm', ['action' => 'example5.php', 'method' => 'post']);
$form->addText(['name' => 'NAME', 'label' => 'Your Name', 'mandatory' => true, 'value' => 'Rudra']);
$form->addText(['name' => 'EMAIL', 'label' => 'Your Email', 'mandatory' => true, 'datatype' => 'email']);
$form->addRadio(['name' => 'GENDER', 'label' => 'Gender', 'mandatory' => true, 'options' => ['array' => ['male', 'female']]]);
$form->addDropdown(['name' => 'EXPERIENCE', 'label' => 'Experience', 'mandatory' => true, 'options' => ['array' => ['fresher', '6 months+', '1 year', '2 years', '3 years', '4 years', '5 years', '5-10 years', '10-15 years', '15+ years']], 'value' => '1 year']);
$form->addTextarea(['name' => 'MESSAGE', 'label' => 'Message', 'rows' => '4', 'cols' => '50', 'mandatory' => true]);
$form->addCollection(['name' => 'work_experience', 'label' => 'Work Experience', 'from' => [['type' => 'text', 'name' => 'company', 'label' => 'Company', 'mandatory' => true], ['type' => 'text', 'name' => 'role', 'label' => 'Role', 'mandatory' => true]]]);
$form->addCaptcha(['name' => 'CAPTCHA', 'secret' => 'blahblah']);
$form->addGroup(['name' => 'buttons', 'direction' => 'inline', 'elements' => [['type' => 'submit', 'name' => 'submit', 'value' => 'Submit'], ['type' => 'reset', 'name' => 'reset', 'value' => 'Reset']]]);
//we can also populate fields from php using setValues() method of the X2Form class
$values = array('work_experience' => array(array('company' => 'Possible Solutions', 'role' => 'Junior Developer'), array('company' => 'Tech Revolution', 'role' => 'Senior Developer')), 'GENDER' => 'male');
$form->setValues($values);
//Alternatively we can also populate individual fields directly
$form->elements['EMAIL']->value = '*****@*****.**';
//array syntax
$form->elements->MESSAGE->value = "I am Rudra!";
//this syntax works as well
$form->finalize();
//handle the form submission
if (isset($_POST['submit']) && $_POST['submit'] == "Submit") {
    $log = $form->processSubmission($_POST);
    if (!logg_ok($log)) {
        $message = '<div class="error">' . logg_msg($log) . '</div>';
        $form->rollBackFileUploads();
    } else {
        $message = '<div class="success">' . logg_msg($log) . '</div>';
    }