使用Javascript从数据库加载数据

Load data from Database using Javascript

本文关键字:加载 数据 数据库 Javascript 使用      更新时间:2023-09-26

我有这个代码来显示从accounts_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>

我在下面的行有问题:

xmlhttp.open("GET","?action=account&q="+str,true);

只是想向你解释一下,我有index.php,那里有很多病例,我有

case "account" :
$q=$_GET["q"];
//code is here to get data from db and return with result..
include($_STR_TEMPLATEPATH."/account_view.php");
break;

因此,当我在account_view.php 中重定向到?action=account&q=str

它会再次显示页面,所以我将有两个页面。

我希望你能清楚地知道这个问题:)

感谢和问候,

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

下面是一个使用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>