从
HTML表单中提取输入值时遇到一些问题.据我所知,我的代码没有任何问题,但我找不到问题所在.
<?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'); ?>
Fatal error: Call to a member function getAttribute() on a non-object in E:\Apache\msg.PHP on line 22
如
doc page中的注释所述,您必须声明getElementById的doctype以按预期执行
t =<<<D <!DOCTYPE html> <form id="frm-send" method="post" action="index.PHP" > ...code continues ...
根据文档,必须为getElementById指定DTD,以了解元素的哪个属性用作唯一标识符.声明doctype可以实现此目的.您也可以使用setIdAttribute显式设置它(不提供DTD),
文档
> http://www.php.net/manual/en/domdocument.getelementbyid.php
> http://www.php.net/manual/en/domelement.setidattribute.php