2013-02-16 91 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

相關問題