使用ajax,php从数据库和自动填充文本框填充

时间:2013-02-05 05:29:27

标签: php ajax

我正面临一个关于这个主题的小问题。当我在上一个textfield中键入id值时,我编写了一个代码来自动填充数据库值的文本框。如果我输入userid,下一个文本字段将自动填充数据库而不刷新在ajax和php.my问题是我无法在我的代码中找到错误。帮助我找出答案。这是我的代码:

      **a.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=iso-8859-1" />
     <title>Untitled Document</title>
   <script type="text/javascript">

   var url = "getagentids.php?param=";

    function handleHttpResponse() {
    if (http.readyState == 4) {
     results = http.responseText.split(",");
       document.getElementById('agfn').value = results[0];
       document.getElementById('agsal').value = results[1];
       document.getElementById('agtel').value = results[2];
       document.getElementById('agid').value = results[3];
     }
    }


    function getagentids() {
    var idValue = document.getElementById("agid").value;
    var myRandom=parseInt(Math.random()*99999999);  // cache buster
    http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
    }


    function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
     @if (@_jscript_version >= 5)
      try {
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
          try {
              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (E) {
              xmlhttp = false;
              }
          } 
     @else
      xmlhttp = false;
     @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
     try {
       xmlhttp = new XMLHttpRequest();
       } catch (e) {
        xmlhttp = false;
        }
       }
       return xmlhttp;
      }


  var http = getHTTPObject(); // We create the HTTP Object


 </script>
    </head>

     <body>
    <form name="schform"> 
      <table> 
     <tr> 
    <td>Contact ID:</td> 
    <td><input id="agid" type="text" name="contactid" onKeyUp="getagentids();"></td> 
   </tr><tr> 
   <td>Tel Number:</td> <td><input id="agtel" type="text" name="contacttel"></td> 
    </tr><tr>
   <td>Name:</td> <td><input id="agfn" type="text" name="contactfullname"></td> 
  </tr><tr>
 <td>Salutation:</td> <td><input id="agsal" type="text" name="contactsalutation"></td> 
 </tr> 
 <tr>  <td><input type="reset" value="Clear"></td> 
<td></td> 
 </tr> 
</table> 
 </form>
 </body>
 </html>



  **getagentids.php**

        <?php

        $link = mysql_connect('localhost', 'arbiocua_mita', 'asd123$'); 
        if (!$link) 
        {
        die('Could not connect: ' . mysql_error());
         }
         mysql_select_db('arbiocua_monomita');

        //$param=intval($_GET['contactid']); 

          if(strlen($param)>0)
          { 
            $result = mysql_query("SELECT ContactFullName, ContactSalutation,   ContactTel FROM contact WHERE ContactID LIKE '$param%'"); 
                if(mysql_num_rows($result)==1) 
                   { 
              while($myrow = mysql_fetch_array($result))
             { 
           $agentname = $myrow["ContactFullName"]; 
            $agenttel = $myrow["ContactTel"]; 
           $agentsal = $myrow["ContactSalutation"]; 
           $agentid = $myrow["ContactID"]; 
           $textout = $agentname.",".$agentsal.",".$agenttel.",".$agentid; 
           } } 
           else { $textout=" , , ,".$param; 
           } } 
        echo $textout;

       ?>


  **database**:

   table name 'contact'
    ContactID   ContactFullName     ContactSalutation   ContactTel

1 个答案:

答案 0 :(得分:0)

$textout=json_encode($textout) 然后echo $textout