Insert, Update and Delete Data in HTML5

Introduction

 
Here we first create a table and insert some data into it. The data will be shown as output and here we will find an edit button and delete button by which we can edit or delete the data like this
 
1.gif
 
Now we will write the code. In order to create the form, use the following procedure.
 
Step 1: First we will create the controls, like TextBox and Button, to insert the data.
  1. <div>  
  2.     <h1>Insert Data, Update Data and Delete Data In HTML5</h1>  
  3.     <input type="hidden" id="id" />  
  4.     <b>First name:</b><input type="text" id="firstName" /><br />  
  5.     <b>Last name:</b><input type="text" id="lastName" /><br />  
  6.     <b>City:</b><input type="text" id="city" /><br />  
  7.     <b>State:</b><input type="text" id="state" /><br />  
  8.     <b>Phone:</b>  
  9.     <input type="text" id="phone" /><br />  
  10.     <b>Email:</b><input type="text" id="email" /><br />  
  11.     <button onclick="InsertData()">Insert Data</button>  
  12.     <button onclick="UpdateData()">Update Data</button>  
  13.     <button onclick="ClearData()">Clear</button>  
  14.     <br />  
  15.     <br />  
  16.     <br />  
  17.     <div id="details"></div>  
  18. </div> 
Here we will use our Id field as a hidden field since in this example it will be incremented automatically.
  1. <script>  
  2.     var id = document.getElementById('id');  
  3.     var firstName = document.getElementById('firstName');  
  4.     var city = document.getElementById('city');  
  5.     var state = document.getElementById('state');  
  6.     var email = document.getElementById('email');  
  7.     var lastName = document.getElementById('lastName');  
  8.     var phone = document.getElementById('phone');  
  9.     var results = document.getElementById('details');  
  10. </script> 
Step 2: Now we will write the following code in the <script> tag like this
 
Here we will create the variables for each field. Now we will create the database like this:
  1. var db = openDatabase("MYDATABASE""1.0""MYDB", 4 * 1024 * 1024);  
  2. var mydata;  
  3. CreateTable();  
  4. function CreateTable() {  
  5.       db.transaction(function (tx) {  
  6.           tx.executeSql("create table if not exists Emp (id INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName TEXT,city TEXT,state TEXT, phone TEXT,email TEXT)");  
  7.       });  
Here we create an Emp table in which we declare id as our primary key, it is also useful in order to delete and update data in the database. Now we write the ShowData() function by which we will show our data
  1. function ShowData() {  
  2.         results.innerHTML = '';  
  3.         db.transaction(function (tx) {  
  4.             tx.executeSql("SELECT * FROM Emp", [], function (tx, result) {  
  5.                 mydata = result.rows;  
  6.                 for (var i = 0, item = null; i < mydata.length; i++) {  
  7.                     item = mydata.item(i);  
  8.                     results.innerHTML +=  
  9.                     '<b>First Name : </b>' + item['firstName'] + ' , ' + '<b>Last Name :</b> ' + item['lastName'] + ' , ' + '<b>City : </b>' + item['city'] + ' , ' + '<b>State :</b>' + item['state'] + ' , ' + '<b>Phone: </b>' + item['phone'] + ' , ' + '<b>Email : </b>' + item['email'] + '      <img height="20" width="20" src="Edit.jpg" onclick="LoadMyData(' + i +  
  10. ')" />' +  
  11.                     '<img height="20" width="20" src="delete.jpg" onclick="deleteRecord(' + item['id'] + ')" /><br/>';  
  12.                 }  
  13.             });  
  14.         });  
  15.     } 
Here we first get the data from the Emp table and then we will show it. Here we will use two images, one for edit and the other for delete. Here we will specify the two functions, one is LoadMyData() and the other one is deleteData().
 
First, we will write the LoadMyData() function that will be helpful to load the data into the TextBoxes:
  1. function LoadMyData(i) {  
  2.      var item = mydata.item(i);  
  3.      firstName.value = item['firstName'];  
  4.      lastName.value = item['lastName'];  
  5.      city.value = item['city'];  
  6.      state.value = item['state'];  
  7.      phone.value = item['phone'];  
  8.      email.value = item['email'];  
  9.      id.value = item['id'];  
  10.  } 
Now we will write the code for deletData(), so when we click on that the particular record it will be deleted from the database:
  1. function deleteData(id) {  
  2.       var deleteStatement = "delete from Emp where id=?";  
  3.       db.transaction(function (tx) {  
  4.           tx.executeSql(deleteStatement, [id], ShowData(), ShowErrorMessage);  
  5.       });  
  6.      ClearData();  
  7. }   
  8. function ClearData() {  
  9.      firstName.value = '';  
  10.      lastName.value = '';  
  11.      city.value = '';  
  12.      state.value = '';  
  13.      email.value = '';  
  14.      phone.value = '';  
  15.      id.value = '';  
Step 3: Now we will write the code to insert the data into the database:
  1. <button onClick="InsertData()">Insert Data</button>  
  2.   
  3. function InsertData() {  
  4.       var insertStatement = "insert into Emp (firstName, lastName,city,state,phone,email) values (?, ?, ?,?,?,?)";  
  5.       db.transaction(function (tx) {   
  6.           tx.executeSql(insertStatement, [firstName.value, lastName.value, city.value, state.value, phone.value, email.value], showdatacleardata, ShowErrorMessage);  
  7.      });  
In this way, we will insert the data into the database.
 
Step 4: Now we will write the code to update the data:
  1. function UpdateData() {  
  2.       var updateStatement = "UPDATE Emp SET firstName = ?, lastName = ?,city=?,state=? ,phone = ?,email=? WHERE id = ?";  
  3.       db.transaction(function (tx) {   
  4.           tx.executeSql(updateStatement, [firstName.value, lastName.value, city.value, state.value, phone.value, email.value, id.value], showdatacleardata, ShowErrorMessage);  
  5.       });  
By which we will easily update the data in the database. So when we click on the Edit Button the data will be shown in the TextBoxes like this, now we will update the data by clicking on the update button.
 
2.gif


Similar Articles