Slip 6 - B) Create employee table as follows EMP (eno, ename,designation, salary). Write Ajax program to select the employees name and print the selected employee’s details.

 Solution:

slip6-p1-q2.html

<html>
<head>
<script>
function showUser(str) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("txtHint").innerHTML = this.responseText;
      }
    };
    xmlhttp.open("GET","slip6-p2-q2.php?q="+str,true);
    xmlhttp.send();
  }
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
  <option value="">Select a person:</option>
  <option value="harsh">harsh</option>
  <option value="lokya">lokya</option>
  <option value="Aryan">Aryan</option>
  <option value="Yash">Yash</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

slip6-p2-q2.php


<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "q3";

$con = mysqli_connect($host, $user, $password,$dbname);


if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<style>
table {
  width: 100%;
  border-collapse: collapse;
}

table, td, th {
  border: 1px solid black;
  padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = $_GET['q'];
mysqli_select_db($con,"ajax");
$sql="SELECT * FROM emp WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Eid</th>
<th>Employeename</th>
<th>Designation</th>
<th>Contact Number</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['designation'] . "</td>";
  echo "<td>" . $row['number'] . "</td>";
  //echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

Post a Comment

0 Comments