PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of René Mansveld   PHP HTML5 Form   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP HTML5 Form
Display and validate a form that uses HTML5
Author: By
Last change:
Date: 1 year ago
Size: 3,872 bytes
 

Contents

Class file image Download
<?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();
?>