2013-02-16 87 views
0

我想从div获得一些价值并将其发送到数据库,我写它,它在我的本地主机上工作,但当我上传源我的主人它停止工作...Proplem与获取元素ID(未捕获TypeError:无法设置属性'innerHTML'为空)

这个错误我在控制台中看到同一页上

Uncaught TypeError: Cannot set property 'innerHTML' of null index.php:700 
request.onreadystatechange 

阿贾克斯那里的div是

<script type="text/javascript"> 
// create the XMLHttpRequest object, according browser 
function get_XmlHttp() { 
    // create the variable that will contain the instance of the XMLHttpRequest object (initially with null value) 
    var xmlHttp = null; 

    if(window.XMLHttpRequest) {  // for Forefox, IE7+, Opera, Safari, ... 
    xmlHttp = new XMLHttpRequest(); 
    } 
    else if(window.ActiveXObject) { // for Internet Explorer 5 or 6 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    return xmlHttp; 
} 

// sends data to a php file, via POST, and displays the received answer 
function ajaxrequest(php_file, tagID) { 
    var request = get_XmlHttp();  // call the function for the XMLHttpRequest instance 

    // create pairs index=value with data that must be sent to server 
    var the_data = 'pg='+document.getElementById('template').innerHTML; 
    var the_data2 = 'memid='+document.getElementById('memid').innerHTML; 
    var the_data3 = 'proid='+document.getElementById('proid').innerHTML; 
    var the_data4 = 'taipei='+document.getElementById('taipei').innerHTML; 

    request.open("POST", php_file, true);   // set the request 

    // adds a header to tell the PHP script to recognize the data as is sent via POST 
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    var post_data = the_data + '&' + the_data2 + '&' + the_data3 + '&' + the_data4; 

    request.send(post_data);  // calls the send() method with datas as parameter 
    // Check request status 
    // If the response is received completely, will be transferred to the HTML tag with tagID 
    request.onreadystatechange = function() { 
    if (request.readyState == 4) { 
     document.getElementById(tagID).innerHTML = request.responseText; 
    } 
    } 
} 
</script> 

的div

<div id="template"> some html here </div> 
<div id="memid" style="display:none;">4</div> 
<div id="proid" style="display:none;">55</div> 
<div id="taipei" style="display:none;">sav</div> 

按钮

<button value="sasasasasa" onclick="ajaxrequest('temp1.php', 'context')"></button> 
+0

那段代码中的哪行是700行?当你通过内置在浏览器中的调试器来浏览时,你会看到什么? – 2013-02-16 14:22:30

+1

为什么它被标记为java和jquery-ajax,因为你不使用java和jquery? – 2013-02-16 14:22:31

+0

可能值得一提的是,在向服务器发送URI编码数据时(例如,在你的'the_data','the_data2'等等变量 - 技术上''='之前使用的名称'),你确实需要使用'encodeURIComponent'。 '也是如此,但是你只能使用26个英文字母,数字和其他几个字符,而不用简单的名字就可以离开。 – 2013-02-16 14:28:46

回答

3

假设线700是

document.getElementById(tagID).innerHTML = request.responseText; 

...则显然不具有与在tagIDid,其中根据示例按钮,一个元件已经显示,将是"context"。事实上,你还没有显示任何HTML定义了一个元素与id"context"

+0

谢谢我忘记添加它时,我将它移动到我的托管XD – 2013-02-16 14:31:52

相关问题