如何使用DOM生成<form>?</form>

时间:2012-02-17 14:47:31

标签: php

可以使用php DOM类 - http://php.net/manual/en/book.dom.php来生成表单元素吗?

1 个答案:

答案 0 :(得分:0)

我试过这个并且有效。

<?php
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->formatOutput = true;
    $html = $dom->createElement('html');
    $dom->appendChild($html);

    //head
    $head = $dom->createElement('head');
    $title = $dom->createElement('title');
    $title->nodeValue = "title of my page";
    $head->appendChild($title);
    $html->appendChild($head);


    //body
    $body = $dom->createElement('body');
    $h1 = $dom->createElement('h1');
    $h1->nodeValue = "Hello, World";
    $body->appendChild($h1);
    $html->appendChild($body);

    //form
    $form = $dom->createElement('form');
    $form->setAttribute("action", "");
    $form->setAttribute("method", "post");

   $checkbox = $dom->createElement("input");
   $checkbox->setAttribute("type", "checkbox");
   $checkbox->setAttribute("name", "myCheckbox");
   $checkbox->setAttribute("value", "someval");
   $form->appendChild($checkbox);

   $body->appendChild($form);

    echo $dom->saveHTML();
?>