我想从 div 中获取一些值并将其发送到数据库,我编写了它并且它在我的本地主机上工作但是当我在我的主机上上传源代码时它停止工作......
我在控制台中遇到这个错误
Uncaught TypeError: Cannot set property 'innerHTML' of null index.php:700
request.onreadystatechange
ajax 在 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>