Core PHP

Server and Client DOM

This PHP example demonstrates how to use the document object model, DOM, on both the server and client sides.

ServerClientDOM.php

<?php

$sHTML = <<<_HTML_DOC
<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net's PHP</title>
  </head>
  <body>
    <script type="text/javascript">
      function HasOverflow(qpTest) {
        var bHasOverflow = ((qpTest.clientWidth < qpTest.scrollWidth) ||
          (qpTest.clientHeight < qpTest.scrollHeight));
        return bHasOverflow;
      }
      // This creates the p tags for printing the overflow message.
      function Initialize() {
        qpBody = document.getElementsByTagName("body")[0];
        qpTest1 = document.createElement('p');
        qpBody.appendChild(qpTest1);
        qpTest2 = document.createElement('p');
        qpBody.appendChild(qpTest2);
        qpOverflow1 = document.getElementById("idOverflow1");
        qpTest1.innerHTML = "idOverflow1 "+
          (HasOverflow(qpOverflow1) ? "has" : "does not have")+" overflow.";
        qpOverflow2 = document.getElementById("idOverflow2");
        qpTest2.innerHTML = "idOverflow2 "+
         (HasOverflow(qpOverflow2) ? "has" : "does not have")+" overflow.";
      }
      window.onload = Initialize;
    </script>
    <p id="idOverflow1" style="width:400px; height:300px;
      background-color:#EEEEFF; color:#444444; overflow:auto;"></p>
    <p id="idOverflow2" style="width:400px; height:300px;
      background-color:#EEEEFF; color:#444444; overflow:auto;"></p>
  </body>
</html>
_HTML_DOC;


$sTextShort = <<<_TEXT_SHORT
1 In the beginning God created heaven, and earth.

2 And the earth was void and empty, and darkness was upon the face of the deep; and the spirit of
God moved over the waters.
_TEXT_SHORT;

$sText = <<<_TEXT
1 In the beginning God created heaven, and earth.

2 And the earth was void and empty, and darkness was upon the face of the deep; and the spirit of
God moved over the waters.

3 And God said: Be light made. And light was made.

4 And God saw the light that it was good; and he divided the light from the darkness.

5 And he called the light Day, and the darkness Night; and there was evening and morning one day.

6 And God said: Let there be a firmament made amidst the waters: and let it divide the waters from
the waters.

7 And God made a firmament, and divided the waters that were under the firmament, from those that
were above the firmament, and it was so.

8 And God called the firmament, Heaven; and the evening and morning were the second day.

9 God also said: Let the waters that are under the heaven, be gathered together into one place:
and let the dry land appear. And it was so done.

10 And God called the dry land, Earth; and the gathering together of the waters, he called Seas.
And God saw that it was good.
_TEXT;

$qDoc = new DomDocument();
$qDoc->LoadHTML($sHTML);
$qElement1 = $qDoc->GetElementById("idOverflow1");
$qElement1->nodeValue = $sTextShort;
$qElement2 = $qDoc->GetElementById("idOverflow2");
$qElement2->nodeValue = $sText;

// Output the HTML
echo $qDoc->SaveHTML();
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.