Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Php how set data in string using html id

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 &lt; and &gt; 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">&lt;p&gt;It is rechnung&lt;/p&gt;</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>&nbsp;</h1><p id="invoice_nummer"></p></td>
    </tr>
    </tbody>
</table>

I can't do it with appendChild() function

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!