你好,我目前正在我的PHP頁面(下面)運行一個JavaScript,它出來與我需要的每個數據是有什麼辦法,我可以連接到MySQL數據庫? (我是新來的JavaScript)從JavaScript運行循環保存在Mysql
<script>
var allItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
for(var i = 0; i < allItems.length; i++) {
var item = allItems[i];
console.log('Current item: %o', item);
}
</script>
'itemsArray來自一個保存功能'
function save(){
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem = {};
var num = document.getElementById("num").value;
newItem[num] = {
"methv": document.getElementById("methv").value
,'q1': document.getElementById("q1").value,
'q2':document.getElementById("q2").value,
'q3':document.getElementById("q3").value,
'q4':document.getElementById("q4").value,
'comm':document.getElementById("comm").value
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));
});
感謝
PS我已經爲數據庫設置了連接
JavaScript的執行在客戶端上。通常,MySQL駐留在服務器上。所以要做到這一點,您必須對服務器執行AJAX請求以存儲/檢索數據。除非這是一個非常奇怪的場景,MySQL將駐留在客戶端機器上,在這種情況下,我不知道JavaScript是否可能,但我猜測事實並非如此。 – Travesty3 2013-03-15 14:42:41