PHP Classes

PHP HTML5 Form: Display and validate a form that uses HTML5

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (12)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 165 All time: 8,882 This week: 89Up
Version License PHP version Categories
html5form 1.0MIT/X Consortium ...7.4HTML, Validation, PHP 7
Description 

Author

This package can display and validate a form that uses HTML5.

It provides functions to add different types of form fields, buttons, or additional HTML tags.

Currently, it supports the field types: bic, checkbox, checkbox group, color, credit card, currency, date, email, file, float, hidden, IBAN, integer, IP, name, password, radio, radio group, range, select, tel, text, textarea, time, URL, zip code.

It also supports button types: submit, reset, and button.

HTML tags are of any type you need to insert into your form, for example, HTML code for a multi-column form or JavaScript to get the visitor's position coordinates.

The HTML generated by this package for the form uses JavaScript to validate the input on-the-fly but also works when the browser does not enable JavaScript support. In this case, the validation will happen only using the rules specified by the generated HTML tags.

The generated JavaScript code assigns the input fields validated by a CSS class that can be either wrong or correct. Developers can define these CSS classes in the stylesheet to show the validation state of each input to the site user.

After the form is submitted, the class can take the submitted form input values to validate the form on the server side. The class returns either 'true' for a completely valid form or an array with the names of the non-valid fields.

Picture of René Mansveld
  Performance   Level  
Name: René Mansveld <contact>
Classes: 3 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 1x

Winner: 1x

Recommendations

Example

<?php
include('src/classes/class.html5form.php');

if (isset(
$_POST['save'])) {
   
$ret = html5form::checkFormData($_POST);
    if (
is_array($ret)) {
        echo
'<pre>Errors = ' . print_r($ret, true) . '</pre>';
    }
}

$oForm = new html5form('.');
$oForm->addCode('<div class="left halfWidth">');
$oForm->addNameField('Name *', 'name', $_POST['name'], null, null, true, 'full name, at least 2 words', array(array('function' => 'str_word_count', 'operator' => '>=', 'compare' => 2)));
$oForm->addCode('</div><div class="right halfWidth">');
$oForm->addTelField('Phone number *', 'phone', $_POST['phone'], null, null, true);
$oForm->addCode('</div><div class="clear"></div><div class="left halfWidth">');
$oForm->addEmailField('Email address *', 'email', $_POST['email'], true);
$oForm->addCode('</div><div class="right halfWidth">');
$oForm->addRadioGroup('Gender *', 'gender', array(array('male', 'Male', ($_POST['gender'] == 'male')), array('female', 'Female', ($_POST['gender'] == 'female'))), true, LabelPositions::before, 'description');
$oForm->addCode('</div><div class="clear"></div>');
$oForm->addTextarea('Request *', 'request', $_POST['request'], null, null, true, 'at least 10 words', array(array('function' => 'str_word_count', 'operator' => '>=', 'compare' => 10)), LabelPositions::before, 'high');
$oForm->addCode('<h3 style="margin: 8px 0 4px 0">Location</h3>');
$oForm->addCode('<div class="left halfWidth">');
$oForm->addNumericField('Latitude', 'lat', NumericFieldTypes::float, $_POST['lat']);
$oForm->addCode('</div><div class="right halfWidth">');
$oForm->addNumericField('Longitude', 'lon', NumericFieldTypes::float, $_POST['lon']);
$oForm->addCode('</div>');
$oForm->addCode('<script>
    function position(pos) {
        document.getElementsByName("lat")[0].value = pos.coords.latitude;
        document.getElementsByName("lon")[0].value = pos.coords.longitude;
    }
    function positionError(error) {
        switch (error.code) {
            case error.PERMISSION_DENIED:
                // alert("Permission denied");
                break;
            case error.POSITION_UNAVAILABLE:
                // alert("Position unavailable");
                break;
            case error.TIMEOUT:
                // alert("Timeout");
                break;
            case error.UNKNOWN_ERROR:
                // alert("Unknown error");
                break;
        }
        getIpLocation();
    }
    function getIpLocation() {
        $.ajax({
            url: "http://ip-api.com/json/",
            method: "get",
            cache: false,
            contentType: "json",
            processData: false,
            success: function(data) {
                var geo = $.parseJSON(data);
                document.getElementsByName("lat")[0].value = pos.coords.latitude;
                document.getElementsByName("lon")[0].value = pos.coords.longitude;
            },
            error: function() {
                alert("Dein aktueller Standort konnte nicht ermittelt werden. Bitte gib ihn selbst so genau wie möglich ein.");
            }
        });
    }
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position, positionError, {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 10
        });
    } else {
        getIpLocation();
    }
</script>'
);
$oForm->addCode('<h3 style="margin: 8px 0 4px 0">Legal</h3>');
$oForm->addCheckbox('Privacy *', 'privacy', '1', false, true, LabelPositions::before, 'I know my data won\'t be used or saved, it will only be validated and returned to me', 'description');
$oForm->addButton('Send', ButtonTypes::submit, 'save', 'Save', '', '', ' ', 'desktop');
$oForm->addButton('Reset', ButtonTypes::reset, '', 'Cancel');
$oForm->display();
?>


Screenshots  
  • Screenshot.png
  • Screenshot2.png
  • Screenshot3.png
  Files folder image Files  
File Role Description
Files folder imagecss (2 files)
Files folder imageimages (3 files)
Files folder imagejs (2 files)
Files folder imagesrc (2 directories)
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file index.php Example Sample page

  Files folder image Files  /  css  
File Role Description
  Accessible without login Plain text file style.min.css Data Compiled stylesheet
  Accessible without login Plain text file style.scss Data Uncompiled stylesheet

  Files folder image Files  /  images  
File Role Description
  Accessible without login Image file cancel.png Data image
  Accessible without login Image file check.png Data image
  Accessible without login Image file ClosePopup.png Data image

  Files folder image Files  /  js  
File Role Description
  Accessible without login Plain text file common.js Data common javascript
  Accessible without login Plain text file cookies.js Data cookies script

  Files folder image Files  /  src  
File Role Description
Files folder imageclasses (2 files)
Files folder imageexternal (1 file)

  Files folder image Files  /  src  /  classes  
File Role Description
  Plain text file class.form.validation.php Class form validation class
  Plain text file class.html5form.php Class html5form class

  Files folder image Files  /  src  /  external  
File Role Description
  Plain text file EmailAddressValidator.php Class email validation class from google

 Version Control Unique User Downloads Download Rankings  
 0%
Total:165
This week:0
All time:8,882
This week:89Up
User Comments (1)
It could have been done much easier.
1 year ago (pataskun)
17%Star