-3
是否可以使用php DOM类 - http://php.net/manual/en/book.dom.php来生成表单元素?如何使用DOM生成<form>?
是否可以使用php DOM类 - http://php.net/manual/en/book.dom.php来生成表单元素?如何使用DOM生成<form>?
我试过这个和工作。
<?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();
?>
当然,你可以在脚下自己射击。我认为用这种OOP风格生成表示层是一种不好的做法 – 2012-07-11 09:31:55
你有什么理由认为你不能? – Quentin 2012-02-17 14:49:46
我还没有看到一个例子,所以我不能说太多。 – Gandalf 2012-02-17 14:53:48
是的,它可以。你试过了吗?你有什么问题? – 2012-02-17 15:35:59