简体   繁体   English

如何将javascript值插入sql数据库,这是Microsoft数据库

[英]how to insert a javascript value into a sql database this is a microsoft database

hi i am tryign to create a store locator and i want to put the users latitude and longitude up into the data base how would i do this i am using javascript to get the results and im going to use php for the database this is the code i am using to get the users location 您好,我尝试创建一个商店定位器,我想将用户的经度和纬度放入数据库中,我将如何执行此操作,我正在使用javascript来获取结果,并且我将使用php作为数据库代码,这是代码我用来获取用户位置

function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else {
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}



function showPosition1(position) {
var locations = [
    ['store1', -2.063150, 2.516503, 4],
    ['store2', -2.064824, 52.518436, 5],
    ['store3', -2.068214, 52.519898, 3],
    ['store4', -2.068558, 52.512769, 2],
    ['store5', -2.070875, 52.510758, 1]
];




lon1 = position.coords.longitude * 0.0174532925;
lat1 = position.coords.latitude * 0.0174532925;
i = 0;

while (i < locations.length) {
    function round(x) {
        return Math.round(x * 10) / 10;
    }
   x.innerHTML += "<br>Distance " + round(calcDist(lon1, lat1, locations[i][1] * 0.0174532925, locations[i][2] * 0.0174532925));
    i++;

}

} }

all my code is working i just want to be able to put the lon 1 and lat1 into my database any ideas would be much appreciated thanks in advance 我所有的代码都在工作,我只想将lon 1和lat1放到我的数据库中,所以任何想法都会非常感谢

You would create another page that does your database insert. 您将创建另一个页面来插入数据库。
Assuming you are using IIS - you write a page in C# or VB that reads the Request.QueryString["latitude"] & Request.QueryString["longitude"]. 假设您使用的是IIS,则可以用C#或VB编写一个读取Request.QueryString [“ latitude”]&Request.QueryString [“ longitude”]的页面。
From there you can use standard .net code to update the SQL Server. 从那里,您可以使用标准.net代码来更新SQL Server。

To send the data from browser to server: 要将数据从浏览器发送到服务器:
On the client side (javascript) you would use the jQuery ajax method to call that page passing your data. 在客户端(javascript),您将使用jQuery ajax方法调用该页面以传递您的数据。 eg 例如

    $.ajax({
    url: 'url_to_server_page.aspx'
    , data: 'latitude=90&longitude90'})
    , success: function (data) { alert('data'); }
});

If you're not using ASP then only your language would change but the basic struture of how you send data to the server is the same - client page sending data to server page. 如果您不使用ASP,则只会更改您的语言,但是将数据发送到服务器的基本结构是相同的-客户端页面将数据发送到服务器页面。 This ajax example uses a default GET for simplicity but since you are updating a database you should do this as a POST. 为了简单起见,此ajax示例使用默认的GET,但由于您正在更新数据库,因此应将其作为POST进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM