我正在研究一個瑣事遊戲,需要更新JavaScript代碼以從數據庫檢索數據,並將數據傳遞給變量。但我不知道從哪裏開始進行改變。我最好的猜測是實現php代碼來與數據庫進行交互並檢索數據。如何從數據庫檢索數據並將其傳遞給JavaScript中的變量?
我的代碼的當前迭代使用內部陣列來創建問題元素
var questions = [{
question: "Which list contains words that are NOT names of shoe types?",
choices: [ "A. Oxford, jelly, boat, clogs, stiletto, mary jane",
"B. Loafer, gladiator, wedge, mule, platform",
"C. Pump, moccasin, wingtip, sneaker, derby, monk",
"D. Chalupa, dogler, hamster, croonley, frankfurt",
],
correctAnswer: 3
}
//the array contains ten of these objects
];
的數據庫中的JavaScript代碼將從被編碼爲這樣retreive變量和值:
Insert into TriviaQuestions(id,questionNum,question,option1,option2,
option3,option4,option5,option6,questionAnswer)
Values('1','1','Which list contains words that are NOT names of shoe types:',
' A. Oxford, jelly, boat, clogs, stiletto, mary jane',
' B. Loafer, gladiator, wedge, mule, platform',
' C. Pump, moccasin, wingtip, sneaker, derby, monk',
' D. Chalupa, dogler, hamster, croonley, frankfurt',
'',
'',
'3')
;
//there are ten questions like these
你需要服務器端代碼和AJAX。 – SLaks
如果您只是在加載時需要javascript中的數據(而不是無刷新地檢索內容),那麼您可以在JavaScript代碼部分中回顯php數據。 – FirstOne
偶然的情況下,你能否鏈接到正在執行此操作的代碼示例? – Duraigo