TY_BCA_AWT_SLIP 5_1



5.    Write an PHP script to search employee name from employee.dat file(Use AJAX concept)



employee.dat file
1 Ramesh
2 Gopal
3 sandip

HTML File
 
<html>
<head><title>Slip 5</title>
<script>
function search()
{

  s=document.getElementById('txt1').value;
  ob=new XMLHttpRequest();

  ob.onreadystatechange=function()
  {
              if(ob.readyState==4 && ob.status==200)
              {
                          document.getElementById('o').innerHTML=ob.responseText;
              }
  }
  ob.open("GET","slip5.php?srch="+s);
  ob.send();
}
</script>
</head>
<body>
<input type=text id=txt1 placeholder="Enter Employee  name"></br>
<input type=button value="Search" onclick="search()">
<h1 id="o">Output</h1>
</body>
</html>

PHP File

<?php
$nm=$_GET['srch'];
$fp=fopen("employee.dat","r");
echo "<table border=1><tr><th>EmpId</th><th>Employee Name</th></tr>";
while($res=fscanf($fp,"%s %s"))
{
  if($res[1]==$nm)
  {
              echo "<tr><td>".$res[0]."</td><td>".$res[1]."</td><tr>";
  }
}
echo "</table>";
?>


No comments:

Post a Comment

Note: only a member of this blog may post a comment.