Php 使用Javascript从数据库加载数据

Php 使用Javascript从数据库加载数据,php,javascript,ajax,Php,Javascript,Ajax,我用这段代码在accounts\u view.php中显示从数据库检索到的数据: <script type="text/javascript"> function showPrice(str) { if (str=="") { document.getElementById("sender_price").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox,

我用这段代码在
accounts\u view.php
中显示从数据库检索到的数据:

<script type="text/javascript">
function showPrice(str)
{
if (str=="")
  {
  document.getElementById("sender_price").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("sender_price").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","?action=account&q="+str,true);
xmlhttp.send();
}
</script>
因此,当我在
account\u view.php中重定向到
?action=account&q=str

它再次显示页面,因此我将有两个页面相互重叠

我希望问题对你来说是清楚的:)

谢谢大家,如果您想使用ajax,我建议您使用从数据库检索数据。所有浏览器的不兼容性都已经考虑在内了,您不必为手动完成这项任务时遇到的所有问题编写代码。基本上,为什么要重新发明已经完美创造的轮子

下面是我使用jquery的代码示例,以及它的易用性

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>some title</title>
<link href="customize.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/black-tie/jquery-ui-1.8rc3.custom.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8rc3.custom.min.js"></script>
<script type="text/javascript">
   function ShowActive(inputString){
     //gives visual that something is happening
     $('#Sresults').addClass('loading');
     //send your data to a php script here i am only sending one variable
     //if using more than one then use json format
     $.post("allactive.php", {queryString: ""+inputString+""}, function(data){
       if(data.length >0) {
         //populate div with results
         $('#Sresults').html(data);
         $('#Sresults').removeClass('loading');
       }
     });
   }
</script>
</head>
<body>
  put your form here ....
  <div id="Sresults" name="Sresults"></div>
</body>

一些头衔
函数ShowActive(输入字符串){
//给人一种发生事情的视觉效果
$('#Sresults').addClass('loading');
//将数据发送到php脚本这里我只发送一个变量
//如果使用多个,则使用json格式
$.post(“allactive.php”,{queryString:“+inputString+”},函数(数据){
如果(data.length>0){
//用结果填充div
$('Sresults').html(数据);
$('Sresults').removeClass('loading');
}
});
}
把你的表格放在这里。。。。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>some title</title>
<link href="customize.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/black-tie/jquery-ui-1.8rc3.custom.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8rc3.custom.min.js"></script>
<script type="text/javascript">
   function ShowActive(inputString){
     //gives visual that something is happening
     $('#Sresults').addClass('loading');
     //send your data to a php script here i am only sending one variable
     //if using more than one then use json format
     $.post("allactive.php", {queryString: ""+inputString+""}, function(data){
       if(data.length >0) {
         //populate div with results
         $('#Sresults').html(data);
         $('#Sresults').removeClass('loading');
       }
     });
   }
</script>
</head>
<body>
  put your form here ....
  <div id="Sresults" name="Sresults"></div>
</body>