DOMDocument - appendXML() 不适用于表单

DOMDocument - appendXML() not working with forms

我在使用 appendXML() 时遇到问题,它不会将表单附加到 HTML。

现在我有这个代码

$domdocument = new DOMDocument();
$domdocument->loadHTML($html);
$domlookup = new DOMXPath($domdocument);
$formDivs = $domlookup->query("//*[contains(@class, 'formClass')]");
$frag = $domdocument->createDocumentFragment();
$frag->appendXML($formHTML);
for ($i = $formDivs->length - 1; $i > -1; $i--) {
    $formDivs->item($i)->parentNode->appendChild($frag);
}
@$html = $domdocument->saveHTML();

该代码运行良好,并附加到 div 和 class "formCLass" 简单 HTML,如文本和 div 文本。但是,当涉及到附加表单时……好吧……那就是另一回事了。

我也尝试过使用

追加
$frag->nodeValue = $formHTML;

而不是

$frag->appendXML($formHTML);

但是还是不行...

正在尝试

$frag->textContent = $formHTML;

会将表单输出为文本,显然这对我不起作用。

我该如何解决这个问题?

表单代码:

<form method="post">
<div class="fb-text form-group field-petname">
<label for="petname" class="fb-text-label">
"Pet Name"<span class="fb-required">*</span>
</label>
<input type="text" placeholder="Your Pet Name" required = "required" aria-required = "true" class="form-control" name="petname" id="petname">
</div>
<div class="fb-text form-group field-petowner">
<label for="petowner" class="fb-text-label">
"Pet owner"<span class="fb-required">*</span>
</label>
<input type="text" placeholder="Insert owner's name" required = "required" aria-required = "true" class="form-control" name="petowner" id="petowner">
</div>
<div class="fb-text form-group field-phone">
<label for="phone" class="fb-text-label">
"Phone number"<span class="fb-required">*</span>
</label>
<input type="text" placeholder="Phone number" required = "required" aria-required = "true" class="form-control" name="phone" id="phone">
</div>
<div>
<div id="goldenmembership">
<i class="fas fa-truck"></i>Free delivery!
</div>
</div>
<div class="fb-select form-group field-brand">
<label for="brand" class="fb-select-label">
What brand would your pet like?<span class="fb-required">*</span>
</label>
<select class="form-control" name="brand" id="brand" required = "required" aria-required = "true">
<option value="3x20" id="brand-1">Hills - 20€</option>
<option value="3x23" id="brand-2">BioPet - 23€</option>
</select>
</div>
</form>

我刚刚使用您的代码和我自己的一些 HTML 将一个非常简单的测试用例组合在一起 HTML,它似乎可以正常追加,如下所示。

您的表格 HTML 是否有可能无效?

如果您仍然遇到问题,能否 post 将您的 HTML 升级到某处,以便我们尝试解决这个问题?

$html = <<<HTML
<html>
    <body>
        <div class="formClass">
            <!-- Placeholder -->
        </div>
    </body>
</html>
HTML;

$formHtml = <<<HTML
<form action="#" method="post">
    <input type="text" id="test"/>
</form>
HTML;

$domdocument = new DOMDocument();
$domdocument->loadHTML($html);
$domlookup = new DOMXPath($domdocument);
$formDivs = $domlookup->query("//*[contains(@class, 'formClass')]");
$frag = $domdocument->createDocumentFragment();
$frag->appendXML($formHtml);
for ($i = $formDivs->length - 1; $i > -1; $i--) {
    $formDivs->item($i)->parentNode->appendChild($frag);
}
$html = $domdocument->saveHTML();

echo $html;

HTML 输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
    <body>
        <div class="formClass">
            <!-- Placeholder -->
        </div>
    <form action="#" method="post">
    <input type="text" id="test">
</form></body>
</html>