PHP code example of alexmigf / forma

1. Go to this page and download the library: Download alexmigf/forma library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexmigf / forma example snippets


$args = [
	'title'        => 'My form title',                    // form title (default: '')
	'classes'      => '',                                 // additional CSS form classes (default: '')
	'action'       => '',                                 // file to process the form data (default: '')
	'method'       => 'post',                             // the form request method (default: 'post')
	'enctype'      => '',                                 // encoding type (default: 'application/x-www-form-urlencoded')
	'callback'     => null,                               // callback function to process the form data (default: null)
	'nonce'        => true,                               // nonce validation, if data is handled by this package (default: false)
	'button_text'  => 'Send',                             // submit button text (default: Send)
	'redirect_uri' => '',                                 // URL for redirection after submit (default: '')
	'messages'     => [ 'error' => '', 'success' => '' ], // form custom  messages (default: null)
];

$forma = new Alexmigf\Forma( $id = 'new-form', $args );

$forma->add_section( $section_id = 'profile' );

$field = [
	'type'     => 'text',
	'id'       => 'first_name',
	'label'    => __( 'First name', 'textdomain' ),
	'value'    => '',
	'rphans', non grouped

$fields = [
	[
		'type'     => 'text',
		'id'       => 'first_name',
		'label'    => __( 'First name', 'textdomain' ),
		'value'    => '',
		' => true,
	],
	[
		'type'     => 'text',
		'id'       => 'company',
		'label'    => __( 'Company', 'textdomain' ),
		'value'    => '',
		'ired' => false,
	],
	[
		'type'     => 'tel',
		'id'       => 'phone_number',
		'label'    => __( 'Phone number', 'textdomain' ),
		'value'    => '',
		'

$forma->add_hidden_field( $name = 'custom_hidden_field', $value = '1' );

$forma->add_nonce_field( $action = 'custom_nonce_action' );

$forma->render();


$data = $database->get_entries();
$forma->add_fields( form_fields( $data ), $section_id = 'profile' );

function form_fields( $data = [] ) {
	return [
		[
			'type'     => 'text',
			'id'       => 'first_name',
			'label'    => __( 'First name', 'textdomain' ),
			'value'    => isset( $data['first_name'] ) ? $data['first_name'] : '',
			't( $data['company'] ) ? $data['company'] : '',
			'=> false,
		],
		[
			'type'     => 'tel',
			'id'       => 'phone_number',
			'label'    => __( 'Phone number', 'textdomain' ),
			'value'    => isset( $data['phone_number'] ) ? $data['phone_number'] : '',
			'

function new_form_process_callback( $request ) {
	// $request contains the data to be processed
	// do your stuff and return bool

	$response = false;
	if ( ! empty( $request ) ) {
		$response = $database->insert( $request );
	}
	return $response;
}