Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOM getElementbyId doesn't work properly

I have a little problem with extracting value of input from an HTML form. As I know, there is nothing wrong with my code, but I cannot find what's the problem.

<?php
error_reporting(E_ALL );
    ini_set('display_errors', 1);

$t =<<<D
<form id="frm-send" method="post"   action="index.php"   >
<input type="text" name="data[postusername]" id="postusername" value="user"  />    
<input type="checkbox"  name="data[save]" id="data[save]" value="1" />   
<input type="hidden" name="secret" id="secret" value="0d35635c0cb11760789de6c4fe35e046311f724b" />
<input type="submit" name="btnSubmit" id="btnSubmit" value="Send"  />   
<input type="hidden" name="data[checkgetrequest]" value="true" id="data[checkgetrequest]" />
<input type="hidden" name="frm-id" value="13448477965028bfb44222d" id="frm-id" />
</form>
<input type="text" id="getfocus_txt_13448477965028bfb44222d" name="getfocus_txt_13448477965028bfb44222d" />


D;
    $dom = new domDocument;
    $dom->loadHTML($t);
    $dom->preserveWhiteSpace = true;
    $frmid = $dom->getElementById('frm-id') ;
    echo  $frmid->getAttribute('value');


?>

It shows me an error:

Fatal error: Call to a member function getAttribute() on a 
non-object in E:\Apache\msg.php on line 22

I'm using XAMPP 1.7.3 on windows 7 . I tested it on my server and it showed me no errors. Any help would be appreciated.

like image 401
undone Avatar asked Aug 13 '12 16:08

undone


2 Answers

From the DOMDocument::getElementById() docs:

For this function to work, you will need either to set some ID attributes with DOMElement::setIdAttribute or a DTD which defines an attribute to be of type ID. In the later case, you will need to validate your document with DOMDocument::validate or DOMDocument::$validateOnParse before using this function.


Since your HTML is only a fragment, it does not specify a DTD, so you are left with dictating the ID attributes yourself. A basic example would look like:

$html = '<div><p id="a">Para A</p><p id="b">Para B</p></div>';

$dom = new DOMDocument;
$dom->loadHTML($html);

// Set the ID attribute to be "id" for each (non-ns) element that has one.
foreach ($dom->getElementsByTagName('*') as $element) {
    if ($element->hasAttribute('id')) {
        $element->setIdAttribute('id', true);
    }
}

$p = $dom->getElementById('b');
echo $p->textContent; // Para B
like image 66
salathe Avatar answered Oct 15 '22 05:10

salathe


As noted in comments on the doc page, you must declare a doctype for getElementById to perform as expected

t =<<<D
<!DOCTYPE html>
<form id="frm-send" method="post"   action="index.php"   >

...code continues ...

Per the documentation, a DTD must be specified for getElementById to understand which attribute of an element is used as the unique identifier. Declaring a doctype accomplishes this. You may also explicitly set this (without giving a DTD) by using setIdAttribute,

Documentation

  • http://www.php.net/manual/en/domdocument.getelementbyid.php
  • http://www.php.net/manual/en/domelement.setidattribute.php
like image 28
Chris Baker Avatar answered Oct 15 '22 04:10

Chris Baker