Javascript 如何将本地存储存储到sql数据库中

Javascript 如何将本地存储存储到sql数据库中,javascript,sql,json,html,local-storage,Javascript,Sql,Json,Html,Local Storage,我有一个应用程序可以用于本地存储,但我想使用JSON将其数据保存到SQL数据库中(例如server=with user=myuser,password=mypass),请您帮助我,请查找下面的代码: HTML <!DOCTYPE html> <html class="no-js"> <head> <meta charset="utf-8"> <title>My App</ti

我有一个应用程序可以用于本地存储,但我想使用JSON将其数据保存到SQL数据库中(例如server=with user=myuser,password=mypass),请您帮助我,请查找下面的代码:

HTML

    <!DOCTYPE html>
    <html class="no-js">
    <head>
        <meta charset="utf-8">
        <title>My App</title>
        <meta name="description" content="">
        <meta name="HandheldFriendly" content="True">
        <meta name="MobileOptimized" content="320">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-stye" content="black">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="cleartype" content="on">

        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="img/touch/apple-touch-       icon-144x144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="img/touch/apple-touch-icon-114x114-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="img/touch/apple-touch-icon-72x72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="img/touch/apple-touch-icon-57x57-precomposed.png">
        <link rel="shortcut icon" href="img/touch/apple-touch-icon.png">

        <!-- Tile icon for Win8 (144x144 + tile color) -->
        <meta name="msapplication-TileImage" content="img/touch/apple-touch-icon-144x144-precomposed.png">
        <meta name="msapplication-TileColor" content="#222222">

        <link rel="stylesheet" href="../css/normalize.css">
        <link rel="stylesheet" href="../css/main.css">
        <script src="../js/vendor/modernizr-2.6.2.min.js"></script>

    </head>
    <body>
 <!-- Add your site or application content here -->
        <div class="wrapper">
            <header>
            <hgroup>
                <h1>iPad Web Application Development</h1>
            </hgroup>
            </header>
            <nav>
            <ul id="tab-bar" class="page-spots">
            <li id="tab-spots">
                <a href="../index.html">Images</a>
            </li>
            <li id="tab-sighting">
                <a href="../video/index.html">Video</a>
            </li>
            <li id="tab-stars">
                <a href="../data/index.html">Database</a>
            </li>
            </ul>
            </nav>
           <h1>Contacts</h1>
<table id="contacts-table">
    <tr id="contacts-head">
        <th>ID</th>
        <th>Name</th>
        <th>Surname</th>
        <th>Email</th>
        <th>Info</th>
        <th>Actions</th>
    </tr>
</table>

<form id="contacts-form"  action="#" method="post" onSubmit="">
    <div class="item text">
        <label>First name:</label>
        <div class="field"><input type="text" name="first_name" required /></div>
    </div>
    <div class="item text">
        <label>Last name:</label>
        <div class="field"><input type="text" name="last_name" required/></div>
    </div>
    <div class="item text">
        <label>Email:</label>
        <div class="field"><input type="text" name="email" required/></div>
    </div>
    <div class="item text">
        <label>Information:</label>
        <div class="field"><textarea rows="7" cols="30" name="information" required></textarea></div>
    </div>
    <div class="button-wrapper">
        <div class="item button">
            <div class="field"><input type="button" id="contacts-op-discard" value="Discard" /></div>
        </div>
        <div class="item button button-default">
            <div class="field"><input type="submit" id="contacts-op-save" value="Save" /></div>
        </div>
    </div>
    <input type="hidden" name="id_entry" value="0" />
</form>

            <footer>
                <p>Stereo Vision Web Application &copy; 2014</p>
            </footer>
        </div>

        <script src="../js/vendor/zepto.min.js"></script>
        <script src="../js/helper.js"></script>
        <script src="../js/app.js"></script>
        <script src="../js/video.js"></script>
        <script src="../js/main.js"></script>
        <script src="../js/data.js"></script>
        <script src="../js/server.js"></script>
        <script scr="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script scr="http://html5form.googlecode.com/svn/trunk/jquery.html5form-1.5-min.js"></script>

    </body>
</html>

我的应用程序
ipadweb应用程序开发
联络 身份证件 名称 姓 电子邮件 信息 行动 名字: 姓氏: 电邮: 资料: 立体视觉网络应用与复制;2014年

JAVASCRIPT代码

var Contacts = {
    index: window.localStorage.getItem("Contacts:index"),
    $table: document.getElementById("contacts-table"),
    $form: document.getElementById("contacts-form"),
    $button_save: document.getElementById("contacts-op-save"),
    $button_discard: document.getElementById("contacts-op-discard"),

    init: function() {
        // initialize the storage index
        if (navigator.onLine) {
            //code
            document.body.style.background="white";
        }else{
            document.body.style.background="#c0c0c0";
        }
        if (!Contacts.index) {
            window.localStorage.setItem("Contacts:index", Contacts.index = 1);
        }

        // initialize the form
        Contacts.$form.reset();
        Contacts.$button_discard.addEventListener("click", function(event) {
            Contacts.$form.reset();
            Contacts.$form.id_entry.value = 0;
        }, true);
        Contacts.$form.addEventListener("submit", function(event) {
            var entry = {
                id: parseInt(this.id_entry.value),
                first_name: this.first_name.value,
                last_name: this.last_name.value,
                email: this.email.value,
                information: this.information.value
            };
            if (entry.id == 0) { // add
                Contacts.storeAdd(entry);
                Contacts.tableAdd(entry);
            }
            else { // edit
                Contacts.storeEdit(entry);
                Contacts.tableEdit(entry);
            }

            this.reset();
            this.id_entry.value = 0;
            event.preventDefault();
        }, true);


        // initialize the table

         if (window.localStorage.length - 1) {
            var contacts_list = [], i, key;
            for (i = 0; i < window.localStorage.length; i++) {
                key = window.localStorage.key(i);
                if (/Contacts:\d+/.test(key)) {
                    contacts_list.push(JSON.parse(window.localStorage.getItem(key)));
                }
            }

            if (contacts_list.length) {
                contacts_list
                    .sort(function(a, b) {
                        return a.id < b.id ? -1 : (a.id > b.id ? 1 : 0);
                    })
                    .forEach(Contacts.tableAdd);
            }
        }


        Contacts.$table.addEventListener("click", function(event) {
            var op = event.target.getAttribute("data-op");
            if (/edit|remove/.test(op)) {
                var entry = JSON.parse(window.localStorage.getItem("Contacts:"+ event.target.getAttribute("data-id")));
                if (op == "edit") {
                    Contacts.$form.first_name.value = entry.first_name;
                    Contacts.$form.last_name.value = entry.last_name;
                    Contacts.$form.email.value = entry.email;
                    Contacts.$form.information.value = entry.information;
                    Contacts.$form.id_entry.value = entry.id;
                }
                else if (op == "remove") {
                    if (confirm('Are you sure you want to remove "'+ entry.first_name +' '+ entry.last_name +'" from your contacts?')) {
                        Contacts.storeRemove(entry);
                        Contacts.tableRemove(entry);
                    }
                }
                event.preventDefault();
            }
        }, true);

    },

    storeAdd: function(entry) {
        entry.id = Contacts.index;
        window.localStorage.setItem("Contacts:"+ entry.id, JSON.stringify(entry));
        window.localStorage.setItem("Contacts:index", ++Contacts.index);
    },

    storeEdit: function(entry) {
        window.localStorage.setItem("Contacts:"+ entry.id, JSON.stringify(entry));
    },

    storeRemove: function(entry) {
        window.localStorage.removeItem("Contacts:"+ entry.id);
    },

    tableAdd: function(entry) {
        var $tr = document.createElement("tr"), $td, key;
        for (key in entry) {
            if (entry.hasOwnProperty(key)) {
                $td = document.createElement("td");
                $td.appendChild(document.createTextNode(entry[key]));
                $tr.appendChild($td);
            }
        }
        $td = document.createElement("td");
        $td.innerHTML = '<a data-op="edit" data-id="'+ entry.id +'">Edit</a> | <a data-op="remove" data-id="'+ entry.id +'">Remove</a>';
        $tr.appendChild($td);
        $tr.setAttribute("id", "entry-"+ entry.id);
        Contacts.$table.appendChild($tr);
    },

    tableEdit: function(entry) {
        var $tr = document.getElementById("entry-"+ entry.id), $td, key;
        $tr.innerHTML = "";
        for (key in entry) {
            if (entry.hasOwnProperty(key)) {
                $td = document.createElement("td");
                $td.appendChild(document.createTextNode(entry[key]));
                $tr.appendChild($td);
            }
        }
        $td = document.createElement("td");
        $td.innerHTML = '<a data-op="edit" data-id="'+ entry.id +'">Edit</a> | <a data-op="remove" data-id="'+ entry.id +'">Remove</a>';
        $tr.appendChild($td);
    },

    tableRemove: function(entry) {
        Contacts.$table.removeChild(document.getElementById("entry-"+ entry.id));
    }
};

Contacts.init();
var联系人={
索引:window.localStorage.getItem(“联系人:索引”),
$table:document.getElementById(“联系人表”),
$form:document.getElementById(“联系人表单”),
$button_save:document.getElementById(“联系人操作保存”),
$button_discard:document.getElementById(“联系人op discard”),
init:function(){
//初始化存储索引
if(navigator.onLine){
//代码
document.body.style.background=“白色”;
}否则{
document.body.style.background=“#c0”;
}
如果(!Contacts.index){
window.localStorage.setItem(“Contacts:index”,Contacts.index=1);
}
//初始化表单
联系人。$form.reset();
Contacts.$button_discard.addEventListener(“单击”),函数(事件){
联系人。$form.reset();
联系人。$form.id\u entry.value=0;
},对);
联系人.$form.addEventListener(“提交”),功能(事件){
变量项={
id:parseInt(this.id\u entry.value),
first\u name:this.first\u name.value,
last_name:this.last_name.value,
电子邮件:this.email.value,
信息:this.information.value
};
如果(entry.id==0){//add
Contacts.storeAdd(条目);
联系人。表格添加(条目);
}
else{//edit
Contacts.storeEdit(条目);
联系人。表格编辑(条目);
}
这是reset();
this.id_entry.value=0;
event.preventDefault();
},对);
//初始化表
if(window.localStorage.length-1){
var contacts_list=[],i,键;
对于(i=0;ib.id-1:0);
})
.forEach(Contacts.tableAdd);
}
}
Contacts.$table.addEventListener(“单击”),函数(事件){
var op=event.target.getAttribute(“数据op”);
如果(/edit | remove/.test(op)){
var entry=JSON.parse(window.localStorage.getItem(“联系人:”+event.target.getAttribute(“数据id”)));
如果(op==“编辑”){
联系人。$form.first\u name.value=entry.first\u name;
联系人。$form.last_name.value=entry.last_name;
联系人。$form.email.value=entry.email;
联系人。$form.information.value=entry.information;
联系人。$form.id\u entry.value=entry.id;
}
否则如果(op==“删除”){
如果(确认('您确定要从您的联系人中删除“'+entry.first\u name+''+entry.last\u name+”)){
联系人。删除(条目);
联系人。表格删除(条目);
}
}
event.preventDefault();
}
},对);
},
storeAdd:函数(条目){
entry.id=Contacts.index;
setItem(“联系人:+entry.id,JSON.stringify(entry));
setItem(“Contacts:index”,++Contacts.index);
},
storeEdit:函数(条目){
setItem(“联系人:+entry.id,JSON.stringify(entry));
},
storeRemove:函数(条目){
window.localStorage.removietem(“联系人:+entry.id”);
},
tableAdd:函数(条目){
var$tr=document.createElement(“tr”),$td,key;
用于(输入项){
if(条目.hasOwnProperty(键)){
$td=document.createElement(“td”);
$td.appendChild(document.createTextNode(条目[key]);
$tr.appendChild($td);
}
}
$td=document.createElement(“td”);
$td.innerHTML='Edit | Remove';
$tr.appendChild($td);
$tr.setAttribute(“id”、“条目-”+entry.id);
联系人:$table.appendChild($tr);
},
tableEdit:函数(条目){