I'm having a problem with appendXML(), it won't append forms to HTML.
Right now I have this code
$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();
The code works well and appends to the div with the class "formCLass" simple HTML, like texts and divs with texts. However, when it comes to appending a form...well...that's a different story.
I have also tried to append using
$frag->nodeValue = $formHTML;
instead of
$frag->appendXML($formHTML);
But it still didn't work...
Trying
$frag->textContent = $formHTML;
will output the form as text and this won't work for me, obviously.
How can I solve this problem?
form code:
<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>
I've just put together a really simple test case using your code, with some of my own HTML, and it seems to be appending okay as you'll see below.
Any chance your form's HTML is invalid?
If you're still having issues, could you post up your HTML somewhere so we can try with that, please?
$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 output:
<!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>