你好,我目前正在我的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