我正在使用以下PHP代码尝试将表单数据附加到html表中(HTML文件已作为Customers.html存在)。
<?php
$myfile = 'Customers.html';
$myfile = realpath($myfile);
$doc = new DOMDocument('1.0');
$doc->loadHTML($myfile);
$doc->formatOutput = True;
$table = $doc->appendChild($doc->createElement('table'));
$tableRow = $table->appendChild($doc->createElement("tr"));
$userName = $tableRow->appendChild($doc->createElement('th'));
$doc->saveHTMLFile("Customers.html");
?>
代码执行正常,但它将生成的html代码转储到body标记之外。我希望PHP创建的表标记作为body标签的子标记附加,但结果如下:
<html>
<body>
</body>
</html>
<table>
<tr>
<th>
</th>
</tr>
</table>
我想要的是:
<html>
<body>
<table>
<tr>
<th>
</th>
</tr>
</table>
</body>
</html>
我如何实现它?
我不想用php创建body标签。
我尝试使用
$body = $doc->getElementsByTagName('body');
$table = $doc->createElement('table');
$body->appendChild($table);
然后将$ table附加到它,它不起作用。
有人可以告诉我我做错了什么。
我是PHP新手,只是学习。 如果需要更多信息,请告诉我。 任何帮助将不胜感激。
提前致谢。
答案 0 :(得分:1)
试试这个
$body = $doc->getElementsByTagName('body')->item(0);
$table = $doc->createElement('table');
$body->appendChild($table);