AJAX HTML Javascript jQuery PHP Example MORE
Need a Website Or Web Application Or Any Help.Contact Us: +91 9437911966 (Whatsapp) Note: Paid Service
Students Tutorial app relased. Download Now.

Send SQLite data to MySQL Online Database Cordova PHP


www/index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <!--<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'" />-->
        <title>Cordova App</title>
        <link rel="stylesheet" href="css/material.indigo-pink.min.css"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
    </head>
    <body>
    <main class="mdl-layout__content mdl-color--grey-100">
    <div class="mdl-grid demo-content" style="width:100%">
        <h4 align="center">Insert Data SQLite</h4>
        <!-- Simple Textfield -->
        <div class="mdl-textfield mdl-js-textfield">
            <input class="mdl-textfield__input" type="text" id="name" >
            <label class="mdl-textfield__label" for="sample1">Name</label>
        </div><br>
        <!-- Numeric Textfield -->
        <div class="mdl-textfield mdl-js-textfield">
            <input class="mdl-textfield__input" type="text"  id="phone">
            <label class="mdl-textfield__label" for="sample2">Phone</label>
        </div><br>
        <!-- Accent-colored raised button with ripple -->
        <button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent" onclick="insertData()">
          Save
        </button>
    </div>
    <div id="tblDiv"></div>
		<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent" onclick="sendData()">
			Send Data to MySql
        </button>
    </div>
  <!-- Script -->
  <script type="text/javascript" src="cordova.js"></script>
  <script type="text/javascript" src="js/material.js"></script>
  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript">
    // window.openDatabase("database-name","version","database description","database size in bytes")
    var db = window.openDatabase("tutorialdb", "1.0", "tutorial database", 1000000); //will create database Dummy_DB or open it
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        // Create Table
        db.transaction(populateDB, errorCB, successCB);
        // Select records
        fetchData();
    }
    function populateDB(tx){
        tx.executeSql('CREATE TABLE IF NOT EXISTS studentsDb (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,phone TEXT NOT NULL)');
    }
    // Fetch all records
    function fetchData(){
        db.transaction(function(tx){
        tx.executeSql("select * from studentsDb order by id desc",[],function(tx1,result){  
                var len = result.rows.length;
                var tblText='<table class="mdl-data-table mdl-js-data-table mdl-data-table--selectable mdl-shadow--2dp" style="width:100%"><tr> <th class="mdl-data-table__cell--non-numeric">Name</th> <th class="mdl-data-table__cell--non-numeric">Phone</th><th class="mdl-data-table__cell--non-numeric">Action</th></tr>';
                for (var i=0; i<len; i++){
                
                            var id = result.rows.item(i).id;
                            var name = result.rows.item(i).name;
                            var phone = result.rows.item(i).phone;
                            tblText +='<tr ><td class="mdl-data-table__cell--non-numeric">'
                            + name +'</td><td class="mdl-data-table__cell--non-numeric">' + phone +'</td><td><a href="manage_student.html?student_id='+id+'"><button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">Manage</button></a></td></tr>';
                }
                tblText +="</table>";
                document.getElementById("tblDiv").innerHTML =tblText;
     
            },errorCB);
        }, errorCB, successCB);
    }
    function insertData(){
        db.transaction(insertQuery, errorCB, successCB);
        alert("Data Added Successfully !");
        window.location.reload();
    }
    function insertQuery(tx){
        var name = document.getElementById('name').value;
        var phone = document.getElementById('phone').value;
        // Insert query
        tx.executeSql('INSERT INTO studentsDb(name,phone) VALUES ("'+name+'","'+phone+'")');
    }
    function errorCB(err) {
        alert("Error processing SQL: "+err.code);
    }
    function successCB() {
           
            //db.transaction(queryDB, errorCB);
    }
	function sendData(){
	                    
        db.transaction(function(tx){
        tx.executeSql("select * from studentsDb",[],function(tx1,result){  
                var len = result.rows.length;
                
                for (var i=0; i<len; i++){
					var id = result.rows.item(i).id;
                    var name = result.rows.item(i).name;
                    var phone = result.rows.item(i).phone;
					$.ajax({
							url: "http://bbtpl.in/save.php",
							type: "POST",
							data: {
								name: name,
								phone: phone
							},
							cache: false,
							success: function(dataResult){
							   
							}
						});
						
					
                }
				 alert("Success");
                },errorCB);
        }, errorCB, successCB);
		
    }
  </script>
 </body>
</html>