0

I am facing a small problem regarding this topic.I had write a code to autofill textboxes from database values when I type a id value in the previous textfield.i.e.if I type userid,the next textfields will autofill from database without refresh in ajax and php.my problem is that I cant find the error in my code.help me to find out.Here is my code:

      **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
Mon
  • 1
  • 6
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя Feb 05 '13 at 05:30
  • use jquery, it will make your code much more easy to use and easy to read – Tucker Feb 05 '13 at 05:43
  • Is there anybody to help me to find out the mistake in my code? – Mon Feb 05 '13 at 07:13

1 Answers1

0

$textout=json_encode($textout) Then echo $textout

Ganesh RJ
  • 942
  • 2
  • 19
  • 31
  • Still I am getting an error message "unexpected echo".Is there any mistake in my query statement? – Mon Feb 05 '13 at 11:23