2012-08-05 37 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