php – DOM getElementbyId无法正常工作
发布时间:2020-12-13 13:18:02 所属栏目:PHP教程 来源:网络整理
导读:从 HTML表单中提取输入值时遇到一些问题.据我所知,我的代码没有任何问题,但我找不到问题所在. ?phperror_reporting(E_ALL ); ini_set('display_errors',1);$t =Dform id="frm-send" method="post" action="index.php" input type="text" name="data[postuser
从
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:Apachemsg.php on line 22 我在Windows 7上使用XAMPP 1.7.3.
如
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |