2012-08-13 11 views
8

Ho un piccolo problema con l'estrazione del valore di input da un modulo HTML. Come so, non c'è niente di sbagliato nel mio codice, ma non riesco a trovare qual è il problema.DOM getElementbyId non funziona correttamente

<?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'); 


?> 

E mi mostra un errore:

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

sto usando XAMPP 1.7.3 su Windows 7. L'ho provato sul mio server e non mi ha mostrato errori. Qualsiasi aiuto sarebbe apprezzato.

+0

Errore confermato: http://codepad.org/ RAknUJ5a –

+0

Ho la stessa cosa su codepad, ma sul mio server funziona correttamente. Codepad è <5.3, IIRC .... @Death, con quale versione di PHP stai lavorando? –

+0

@Chris php 5.3.1 --- – undone

risposta

4

Come notato nei commenti sul doc page, è necessario dichiarare un doctype per getElementById per funzionare come previsto

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

...code continues ... 

Per la documentazione, un DTD deve essere specificato per getElementById per capire quale attributo di un elemento viene utilizzato come identificativo univoco. Dichiarare un doctype porta a termine questo. È anche possibile impostare in modo esplicito questo (senza dare un DTD) utilizzando setIdAttribute,

Documentazione

+0

Puoi spiegare perché il mio codice funziona sul server? – undone

+0

No, non posso spiegare perché sul mio server (con PHP 5.4), il doctype non è richiesto. Ho cercato di scoprirlo, perché penso che sia strano che io ** non debba dichiarare un doctype. Dov'è l'istanza PHP del mio server che riceve la DTD quando viene chiamato 'loadHTML'? Indovinare? –

+0

il mio server è php 5.2 e ho provato con un altro php 5.3 e tutto funziona !!! – undone

6

Dal 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.


Dal momento che il codice HTML è solo un frammento, non specifica un DTD, così si sono lasciati con dettare l'ID attributi da soli. Un esempio di base sarebbe simile:

$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 
Problemi correlati