Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7564219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:51:25+00:00 2026-05-30T13:51:25+00:00

I’m wanting to create a form builder that creates HTML forms programmatically with PHP,

  • 0

I’m wanting to create a form builder that creates HTML forms programmatically with PHP, but I’m wanting it to be object-orientated. This lead me to looking at PHP’s DOM extension, but it seems to work on the concept of full HTML (and XML) documents rather than portions of the DOM.

Therefore, is it possible in PHP to create HTML in an object-orientated way? For example, my pseudo-code may look as follows:

<?php

$form = new HTMLElement('form', array('method' => 'post', 'action' => 'contact.php'));

$fieldset = $form->addElement(new HTMLElement('fieldset'));
$fieldset->addElement(new HTMLElement('input', array(
    'type' => 'text',
    'name' => 'name'
)));
$fieldset->addElement(new HTMLElement('input', array(
    'type' => 'email',
    'name' => 'email'
)));
// and so on...

echo $form->asHTML();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:51:26+00:00Added an answer on May 30, 2026 at 1:51 pm

    You can use DOMDocument (requires PHP 5, btw):

    <?php
    
    $html = new DOMDocument('1.0','iso-8859-1');
    $html->formatOutput = true;
    
    $form = $html->createElement('form');
    
    $fieldset = $html->createElement('fieldset');
    
    $name = $html->createElement('input');
    $name->setAttribute('type', 'text');
    $name->setAttribute('name', 'name');
    
    $email = $html->createElement('input');
    $email->setAttribute('type', 'text');
    $email->setAttribute('name', 'email');
    
    $fieldset->appendChild($name);
    $fieldset->appendChild($email);
    
    $form->appendChild($fieldset);
    
    $html->appendChild($form);
    
    echo html_entity_decode($html->saveHTML());
    

    http://codepad.org/sK0j6zD3

    Which outputs:

    <form><fieldset>
    <input type="text" name="email"><input type="text">
    </fieldset></form>
    

    A more complete document:

    <?php
    
    class br extends DOMElement {
        function __construct() {
            parent::__construct('br');
        }
    }
    
    $page = new DOMDocument();
    $page->normalizeDocument();
    $page->formatOutput = true;
    
    $html = $page->createElement('html');
    $head = $page->createElement('head');
    $title = $page->createElement('title');
    $body = $page->createElement('body');
    $form = $page->createElement('form');
    $fieldset = $page->createElement('fieldset');
    $name = $page->createElement('input');
    $email = $page->createElement('input');
    $submit = $page->createElement('input');
    
    $title_text = $page->createTextNode('Page Title Here');
    $title->appendChild($title_text);
    
    $head->appendChild($title);
    
    $html->appendChild($head);
    
    $name->setAttribute('type', 'text');
    $name->setAttribute('name', 'name');
    
    $email->setAttribute('type', 'text');
    $email->setAttribute('name', 'email');
    
    $submit->setAttribute('type','submit');
    $submit->setAttribute('value','Submit');
    
    $fieldset->appendChild($page->createTextNode('Name: '));
    $fieldset->appendChild($name);
    $fieldset->appendChild(new br);
    $fieldset->appendChild($page->createTextNode('Email: '));
    $fieldset->appendChild($email);
    $fieldset->appendChild(new br);
    $fieldset->appendChild($submit);
    
    $form->appendChild($fieldset);
    
    $body->appendChild($form);
    
    $html->appendChild($body);
    
    $page->appendChild($html);
    
    echo "<!DOCTYPE html>\n" . html_entity_decode($page->saveHTML());
    

    http://codepad.org/uRNoW3nN

    Which outputs:

    <!DOCTYPE html>
    <html>
    <head><title>Page Title Here</title></head>
    <body><form><fieldset>Name: <input type="text" name="name"><br>Email: <input type="text" name="email"><br><input type="submit" value="Submit">
    </fieldset></form></body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.