开发者

Text form using Ajax help

how to read data from text field using AJAX and send it to php? I want to do that in a single form window with out navigating to other form.

Instead of having a submit button it would be awesome if we had live search.

The data i read is fetched from database..

Can any one tell me which are good tutorials to find these Ajax stuff?

Text form using Ajax help

//what i have done so far
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
 document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest开发者_如何学JAVA();
 }
 else
 {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
 }
</script>
</head>
<body>
<div id="txtHint"><b>ID info will be listed here.</b></div>

</body>
</html> 

and PHP file something like this

 <?php
$q=$_GET["q"];

Connect to DB and select DB

 $value = $_POST[''];// value from text field

$sql="SELECT * FROM parentid WHERE id = '$value'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Title</th>
<th>number</th>
<th>abc</th>
<th>institution</th>
 </tr>";

 while($row = mysql_fetch_array($result))
   {
   echo "<tr>";
  echo "<td>" . $row['title'] . "</td>";
 echo "<td>" . $row['number'] . "</td>";
 echo "<td>" . $row['abc'] . "</td>";
 echo "<td>" . $row['institution'] . "</td>";
  echo "</tr>";
  }
  echo "</table>";

 mysql_close($link);
?> 


consider using jQuery

then your code might look something like

<script>
    $('#txtHint').change(function(){
       $.get('getuser.php?q=' + $('#txtHint').val(), function() { /* maybe do something here? */ });
    });
</script>

then the php could be

<?php
$q=$_GET["q"];

//Connect to DB and select DB


$sql="SELECT * FROM parentid WHERE id = '$q'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Title</th>
<th>number</th>
<th>abc</th>
<th>institution</th>
 </tr>";

 while($row = mysql_fetch_array($result))
   {
   echo "<tr>";
  echo "<td>" . $row['title'] . "</td>";
 echo "<td>" . $row['number'] . "</td>";
 echo "<td>" . $row['abc'] . "</td>";
 echo "<td>" . $row['institution'] . "</td>";
  echo "</tr>";
  }
  echo "</table>";

 mysql_close($link);
?> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜