2012-08-05 19 views
1

我有以下HTML元素的值,我需要插入到數據庫。如何使用HTML文件中的兩個參數編寫AJAX GET請求?

<p>You scored :: </p><p id="txtScore1"></p> 
<input type="text" placeholder="Your Name" id="name"> 

這是在W3schools網站上建議的示例javascript代碼,以完成任務。

xmlhttp.open("GET","demo_get2.asp?fname=Henry&lname=Ford",true); 
xmlhttp.send(); 

所以,我想下面的線在我的JavaScript文件:

xmlhttp.open("GET","insert.php?q=&name=" + encodeURIComponent(document.getElementById('name').value + "&txtScore1=" + encodeURIComponent(document.getElementById('txtScore1').innerHTML),true); 
xmlhttp.send(); 

它不工作,並且在鍍鉻控制檯錯誤給出未捕獲的語法錯誤。意外標識符(重複兩次)。

您能否提出我正在做的錯誤?

+0

谷歌瀏覽器會告訴你在行你所得到的錯誤。張貼這些線,以便可以看到他們有什麼問題。 – zizoujab 2012-08-05 14:23:28

+0

問題通過@joar解決 – 2012-08-05 14:49:31

回答

4

這是一個語法錯誤。

xmlhttp.open("GET","insert.php?q=&name=" + encodeURIComponent(document.getElementById('name').value + "&txtScore1=" + encodeURIComponent(document.getElementById('txtScore1').innerHTML),true); 

應該

xmlhttp.open("GET","insert.php?q=&name=" + encodeURIComponent(document.getElementById('name').value) + "&txtScore1=" + encodeURIComponent(document.getElementById('txtScore1').innerHTML),true); 
//                         ↑ 
//                      Added parenthesis            
+0

非常感謝。 – 2012-08-05 14:44:12

+0

很高興我能幫到你,如果你想標記這個答案屁股正確的話,請點擊箭頭旁邊的複選框。 – joar 2012-08-05 14:45:32

+1

是的。它是正確的。感謝您向我學習電子郵件,因爲我是新手入手。 – 2012-08-05 14:48:23