douzhao9608 2017-02-24 12:57
浏览 133
已采纳

当页面完全加载时,Div清除

I am using a JavaScript & Ajax to make a php call to return data from my database. The call returns the data as it should, but when the page fully loads the <div> tags value is cleared.

What do I need to change in my syntax so that the div tag retains the value echo from the php file? I checked the Console and it is only showing page load info and nothing related to this issue (at least not that I saw).

<form id="form1" method="post">
<div id="results">&nbsp;</div>
<div style="padding-top: 10px;"><input type="submit" value="Submit" onclick="MakeQuery()" /></div>
<script type="text/javascript">
var xhttp;
function MakeQuery()
{
 if (window.XMLHttpRequest) {    
  xhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { 
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
} 
    xhttp.onreadystatechange = function(){ if (xhttp.readyState == 4 && xhttp.status==200)
    { document.getElementById("results").innerHTML = xhttp.responseText; } }
     xhttp.open("GET", "SQLQuery.php", true); 
     xhttp.send();
}
</script>
</form>
  • 写回答

1条回答 默认 最新

  • douyue8685 2017-02-24 13:10
    关注

    I think you need to prevent the actual form submit before using AJAX:

    var xhttp;
    
    function MakeQuery(event) {
      event.preventDefault(); // Cancel form submit, use AJAX
    
      if (window.XMLHttpRequest) {
    
        xhttp = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
          document.getElementById("results").innerHTML = xhttp.responseText;
        }
      }
      xhttp.open("GET", "SQLQuery.php", true);
      xhttp.send();
    }

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题