使用php和ajax来实现通过输入框的输入值查询数据库,并把输出结果显示在页面,但是查询结果没有随数据库数据的变化而更新,我希望点击查询按钮后,如果数据库数据变化,页面也会变化,问题出在哪里呢?求大神帮忙!!!!!

解决方案 »

  1.   

    index.html的代码:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0,user-scalable=0"/><title>Read Result</title><script type="text/javascript">
           function checkKpMeterNum(num)
        {
            arrNum = num.split("");
            if (arrNum.length != 11) 
            return false;
            LSum = 0;
            for (i=0;i<11;i++)
            {
                LDigit = arrNum[i];
                LDigit = LDigit * (1+( i % 2));
                LSum = LSum + parseInt(LDigit / 10) + (LDigit % 10);
            }
            return LSum % 10 == 0;
        }var xmlobj;
    function CreateXMLHttpRequest() 
    {  
        if(window.ActiveXObject)  
        {  
             xmlobj = new ActiveXObject("Microsoft.XMLHTTP"); 
        }  
        else if(window.XMLHttpRequest)   
        {  
             xmlobj = new XMLHttpRequest(); 
        } 
    }      function showtb(id)
        {
            var tb = document.getElementById(id);
            var checktext = document.getElementById("MeterNum").value;
            if(checktext==""||checktext==null)
            {
                 tb.style.display ="none";
                alert ("Please Input MeterNum!");
                
            }  
            else if (checkKpMeterNum(checktext)==false)
            {
                 tb.style.display ="none";
                alert("MeterNum is Illegal!");
                
            }
            else
            {
                if (tb.style.display == "none")
                {
                tb.style.display ="";
                
              CreateXMLHttpRequest();                 
      
         var showurl = "ReadResult.php?MeterNum=" + checktext +"&Date="+new Date().getTime(); 
    xmlobj.open("GET", showurl, true);
     
         xmlobj.onreadystatechange =  function (){  
         if(xmlobj.readyState == 4 && xmlobj.status == 200)  
         {  
        
             tb.innerHTML = xmlobj.responseText; 
            
         } 
    } ; 
        
         xmlobj.send(null);
             
               }
      
            }
      }
     
      
    </script>
    </head><body><div style="text-align: center;">
        <a><img src="logo1.png" style="max-width: 100%;"/></a>
    </div> 
            <div  style="text-align: center;">
             <form id="form2" method="get" action="">
                <a style="font-size: 1.1em;font-weight: bold;">Meter No. :</a> 
                <input type="text"  id="MeterNum"  /> 
                <input style="font-size: 1.1em;font-weight: bold;width:auto;height:100%;"  type="button" value="search"   onclick="showtb('d1')" />
             </form>
        </div>
        <div id="d1" style="text-align: center;display: none;" >
      
        </div>
        
    </body>
    </html>
      

  2.   

    readresult.php页面代码:<?php 
    $LANG_MODULES = array('SMARTvend_lang');   
    include_once('config/config.inc.php');$MeterNum = trim($_GET['MeterNum']);
    //echo $MeterNum;exit;
    $dbo = Db::getInstance();$query ="SELECT a.RegCode,a.MeterNum,a.ExecDate,a.COperator,b.FullName,";
    $query .="  sum(case when a.FieldName='03473002' then a.FieldValue end ) as Frequency,";
    $query .="  sum(case when a.FieldName='03000102' then a.FieldValue end) as LastMonthkWh, ";
    $query .="  sum(case when a.FieldName='04000006' then a.FieldValue end) as LastMonthMD";
    $query .="  FROM amr_read a,bz_meter b where a.MeterNum='".$MeterNum."' and a.RegCode=b.Code ";//echo $query;exit;
    $cursor = $dbo->ExecuteS($query,"json");
    //echo $cursor[0]['Frequency'];exit; /*$conn=mysql_connect("localhost:6299", "root", "svr2010");             
     $result=mysql_db_query("smartvend", "SELECT a.RegCode,a.MeterNum,a.ExecDate,a.COperator, 
            sum(case when a.FieldName='03473002' then a.FieldValue end ) as Frequency, 
            sum(case when a.FieldName='03000102' then a.FieldValue end) as LastMonthkWh,  
            sum(case when a.FieldName='04000006' then a.FieldValue end) as LastMonthMD
            FROM amr_read a,bz_meter b where a.MeterNum='$MeterNum' and a.RegCode=b.Code", $conn);
        
        echo $result;exit;
        // 获取查询结果
        $row=mysql_fetch_row($result);*/
       
      echo "<table width='100%' id='tb1' >
           <tr >
             <td style='font-size: 0.875em; text-align: right;font-weight: bold;' width='45%'>Registe Code:</td>
               <td style='font-size: 0.875em; text-align: left;font-weight: bold;'>".$cursor[0]['RegCode']."</td>
          </tr>
          <tr>
                <td style='font-size: 0.875em;text-align: right;font-weight: bold;' width='45%'>Meter No.:</td>
               <td style='font-size: 0.875em; text-align: left;font-weight: bold;'>".$cursor[0]['MeterNum']."</td>
          </tr>
          <tr>
               <td style='font-size: 0.875em;text-align: right;font-weight: bold;' width='45%'>Customer Name:</td>
              <td style='font-size: 0.875em; text-align: left;font-weight: bold;'>".$cursor[0]['FullName']."</td>
          </tr>
          <tr>
               <td style='font-size: 0.875em; text-align: right;font-weight: bold;' width='45%'>Frequency:</td>
               <td style='font-size: 0.875em; text-align: left;font-weight: bold;'>".$cursor[0]['Frequency']." kWh</td>
          </tr>
          <tr>
              <td style='font-size: 0.875em; text-align: right;font-weight: bold;' width='45%'>Last Month kWh:</td>
                <td style='font-size: 0.875em; text-align: left;font-weight: bold;'>".$cursor[0]['LastMonthkWh']." kWh</td>
          </tr>
          <tr>
               <td style='font-size: 0.875em; text-align: right;font-weight: bold;' width='45%'>Last Month MD:</td>
              <td style='font-size: 0.875em; text-align: left;font-weight: bold;'>".$cursor[0]['LastMonthMD']." kWh</td>
          </tr>
          <tr>
                <td style='font-size: 0.875em; text-align: right;font-weight: bold;' width='45%'>Read Date:</td>
                <td style='font-size: 0.875em; text-align: left;font-weight: bold;'>".$cursor[0]['ExecDate']."</td>
          </tr>
          <tr>
               <td style='font-size: 0.875em; text-align: right;font-weight: bold;' width='45%'>Operator:</td>
                <td style='font-size: 0.875em; text-align: left;font-weight: bold;'>".$cursor[0]['COperator']."</td>
          </tr> 
      </table>"; exit; 
            
        
    ?>