Search code examples
phpdomdocumentgetelementsbytagname

PHP DOMDocument getElementsByTagName returns incomplete elements?


I'm using PHP 5.3. I have the following code:

view1.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
      <form method="post">
         <span>Hello this is some text</span>
         <input type="text" name="input1" value="mystuf"/>
         <p>Blah blah this is boring</p>
         <input type="text" name="input2" value="hello"/>
         <img src="image-of-a-kangaroo.png" />
         <input type="text" name="input3" />
         <ul>
            <li>Buy brocolli</li>
            <li>Buy oregano</li>
            <li>Buy milk</li>
         </ul>
         <input type="text" name="input4" />
         <textarea name="input100"></textarea>
         <input type="text" name="input101" />
         <p><strong>Yes, I like pizza!</strong><span>But my porcupine gets sick eating pizza.</span></p>
      </form>
</body>
</html>

index.php

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->loadHTMLFile('view1.html');
$input = $xmlDoc->getElementsByTagName('input');
foreach ($input as $i) {
        echo $i->nodeValue, PHP_EOL;
}

How come $i->nodeValue always gives empty or no result?


Solution

  • Doing an echo $i->getAttribute('value') showed me the values I was looking for.