Web Technology Lab Manual
PROGRAM CODE WITH OUTPUT
Here we have created “emp” directory in htdocs directory contain following files
1. index.html
2. insert.php
3. display.php
4. config.php
1. index.html
<!DOCTYPE html>
<html>
<head>
<title>PHP Databse Example with Ajax </title>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>
</head>
<body>
<h1> Enter Employee Details </h1>
<form method="post" action="insert.php">
<input type="text" id="name" name="name" placeholder="Enter Name" />
<input type="text" id="age" name="age" placeholder="Enter Age" />
<input type="text" id="city" name="city" placeholder="Enter City" />
<button> Save Data </button>
</form>
<p id="result">
</p>
<p id="result"></p>
Computer Department, AISSMS IOIT                                                      Page 64
                                                                Web Technology Lab Manual
<a href="display.php">Display</a>
<!--jquery and ajax code-->
<script>
$("form").submit(function(e){
e.preventDefault();
$.post(
"insert.php",
{
name: $("#name").val(),
age: $("#age").val(),
city: $("#city").val()
},
function(result)
{
if(result == "success")
{
          $("#result").html("Data Inserted Successfully..!");
}
else
{
          $("#result").html("Error Occured!");
}
}
);
});
</script>
</script>
</body>
Computer Department, AISSMS IOIT                                                 Page 65
                                                                    Web Technology Lab Manual
<html>
2. insert.php
<?php
$name = $_POST['name'];
$age = $_POST['age'];
$city = $_POST['city'];
$con = new mysqli('localhost', 'root', '', 'emp');
if($con->connect_error)
{
echo("Error");
}
$stmt = $con->prepare("insert into users(name,age,city) values (?,?,?)");
$stmt->bind_param("sis",$name,$age,$city);
Computer Department, AISSMS IOIT                                                     Page 66
                                                                 Web Technology Lab Manual
if($stmt->execute())
{
echo("success");
}
else
{
echo("fail");
}
?>
in below image URL you can see that without refreshing whole page only part of page refreshed.
3. display.php
<?php
include_once("config.php");
Computer Department, AISSMS IOIT                                                     Page 67
                                                                    Web Technology Lab Manual
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is
deprecated
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); // using
mysqli_query instead
?>
<html>
<body>
<a href="index.html">Add New Data</a><br/><br/>
         <table width='80%' border=0>
         <tr bgcolor='#CCCCCC'>
                 <td>Name</td>
                 <td>Age</td>
                 <td>CIty</td>
         </tr>
         <?php
         //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need
to use mysqli_fetch_array
         while($res = mysqli_fetch_array($result)) {
                 echo "<tr>";
                 echo "<td>".$res['name']."</td>";
                 echo "<td>".$res['age']."</td>";
                 echo "<td>".$res['city']."</td>";
                 echo "</tr>";
         }
         ?>
         </table>
Computer Department, AISSMS IOIT                                                        Page 68
                                                              Web Technology Lab Manual
</body>
</html>
4. config.php
<?php
$host = 'localhost';
$dbname = 'emp';
$dabUser = 'root';
$dbPass = ' ';
$mysqli = mysqli_connect($host, $dbUser, $dbPass, $dbname);
?>
Computer Department, AISSMS IOIT                                               Page 69