0

divから値を取得してデータベースに送信したいのですが、それを書き込んでローカルホストで機能しましたが、ホストにソースをアップロードすると機能しなくなりました...

このエラーはコンソールに表示されます

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

divが存在する同じページのajax

   <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>
4

1 に答える 1

3

700行目が

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

id...次に、 inを含む要素がないことは明らかです。これは、tagID表示したボタンの例によれば、になります"context"。実際、。を使用して要素を定義するHTMLは表示されていませんid "context"

于 2013-02-16T14:21:40.873 に答える