如何写一个与html文件,采取了两个参数AJAX GET请求? [英] How to write a AJAX GET request with two parameters taken from html file?

查看:236
本文介绍了如何写一个与html文件,采取了两个参数AJAX GET请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的HTML元素的值,我需要插入到数据库。

I have the following HTML elements whose values I need to INSERT to a database.

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

这是得到任务完成的样品在W3School网站的JavaScript code建议。

This is the sample javascript code suggested at W3schools site to get the task done.

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

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

So I tried the following lines in my javascript file:

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

它不工作,并且在镀铬控制台中的错误给定为未捕获的语法错误。意外的标识符(重复两次)。

您可以请建议我做错误?

Can you please suggest the error I'm doing?

推荐答案

这是一个语法错误。

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                                               

这篇关于如何写一个与html文件,采取了两个参数AJAX GET请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆