I have long html text. Pass it from js to php. I need change ID content with my text.
I tried did it like this:
$html = new DOMDocument();
$html->loadHTML($codeText);
$html->getElementById('second_head')->nodeValue = $leadAddressTemplate;
$html->getElementById('rechnung_div')->nodeValue = $rehnungTemplate;
$res = $html->saveHTML();
It works but have problem - my <> chars system replaces to < and > and system adds <html><body> to my text.
How can I fix it? Maybe there are some flags for it?
For example, input is :
<tr>
<td colspan="2" class="invoice-products" width="100%">
<div id="rechnung_div"></div>
</td>
</tr>
And it try do it:
$html->getElementById('rechnung_div')->nodeValue = '<p>It is rechnung</p>';
As result i have this:
<tr>
<td colspan="2" class="invoice-products" width="100%">
<div id="rechnung_div"><p>It is rechnung</p></div>
</td>
</tr>
Now I can set for example
element:
$appended = $html->createElement('p', 'It is rechnung');
$html->getElementById('rechnung_div')->nodeValue = '';
$rechnung_div->appendChild($appended);
But how insert table , for example like this?
<table>
<tbody><tr>
<td style="width: 40%;vertical-align: baseline;"><h1 id="invoice_type">Rechnung</h1>
<p id="invoice_title">gtrgtrgrtgtr</p></td>
<td style="width: 60%;text-align: right;vertical-align: bottom;"><h1> </h1><p id="invoice_nummer"></p></td>
</tr>
</tbody>
</table>
I can't do it with appendChild() function
Rolling together the notes from the comments:
$parent = <<<_E_
<tr>
<td colspan="2" class="invoice-products" width="100%">
<div id="rechnung_div"></div>
</td>
</tr>
_E_;
$template = <<<_E_
<p>I am a template</p>
_E_;
$temp = new DOMDocument();
$temp->loadHTML($template, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// get the document's root element
$temp_root = $temp->documentElement;
$html = new DOMDocument();
$html->loadHTML($parent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// import the template into the current document
$node = $html->importNode($temp_root, true);
$html->getElementById('rechnung_div')->appendChild($node);
echo $html->saveHTML();
Output:
<tr>
<td colspan="2" class="invoice-products" width="100%">
<div id="rechnung_div"><p>I am a template</p></div>
</td>
</tr>