从数据库填充数据并在文本框中显示文本框而不提交表单

时间:2014-10-19 18:00:59

标签: javascript php jquery html mysql

请检查下面的代码我没有得到输出

我想通过键入文本字段来查询数据库,并且所有与文本字段相关的匹配数据都应该通过Ajax填充在其他空字段上,而无需在同一页面上提交表单,所以我尝试了这段代码但是它不起作用。

我看到的教程工作得很完美我不知道我做了什么错,因为我没有得到结果....请检查以下链接的教程,你可以理解我确切需要...

我通过查看本教程http://www.crackajax.net/popform.php

实现了这一点

我是新手,请帮我解决这个问题.......

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
<script>
var url = "index.php?param="; 
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 handleHttpResponse() 
{ 
if (http.readyState == 3) 
{ 
results = http.responseText.split(",");
document.getElementById('agtel').value = results[0]; 
document.getElementById('agfn').value = results[1]; 
document.getElementById('agid').value = results[2]; 
} 
} 
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>Person Name1:</td> 
<td><input id="agtel" type="text" name="contacttel"></td> 
</tr> 
<tr> 
<td>Person Name2:</td> 
<td><input id="agfn" type="text" name="contactfullname"></td> </tr> 

<td><input type="reset" value="Clear">
</td> 
<td></td> 
</tr> 
</table> 
</form>



<?php
// Create connection
$con=mysqli_connect("localhost","root","root","spikadb");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

 if(strlen($param)>0)
 { 
 $result = mysql_query("SELECT * FROM user WHERE flat_no LIKE '$param%'"); 
 if(mysql_num_rows($result)==1) { while($myrow = mysql_fetch_array($result))
 { 
 $personname1 = $myrow["name"]; 
 $personname2 = $myrow["name2"]; 
 $flatno = $myrow["flat_no"];

 $textout .= $personname1.",".$personname2.",".$flatno;
 } 
    } 
    else 
    { 
    $textout=" , , ,".$param; 
    } 
        } 
        echo $textout;
 mysqli_close($con);
?> 




</body>
</html>

0 个答案:

没有答案