Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Javacript interface with SQL

Author  Topic 

parrot
Posting Yak Master

132 Posts

Posted - 2011-04-02 : 12:21:17
Is there any way to pass variables from javascript in a web page to an SQL routine? I am using a navigator.geolocation javascript routine to get the gps location of IP addresses accessing my web site and I want to store these in an SQL table. I cannot figure out how to pass these variables to an SQL table in my apsx C# program. It probably isn't possible but I thought I would ask anyway.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-02 : 16:20:21
Sure it's possible. My .Net and JavaScript skills are pretty rusty but I used to do this all the time. If you have an HTML form of some kind, even if it's based on .Net controls, you can set their values directly using JavaScript. If you can't or don't want to use existing form controls, create something like <input type="hidden" name="GPS"> on the form. Have your script assign the value to that control, then submit the form (form.submit() method). Your ASPX code can then read this value from the Request object and send it to your SQL Server.

Here's an example using JavaScript to check form element values. It's pretty easy to modify it to change an input's value:

http://www.w3schools.com/js/js_form_validation.asp
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2011-04-06 : 14:32:14
Thank you for your reply. I can see how javascript can pass data to the form. The problem I am having is that I am using C# as a background language in my web form and I cannot access the aspx fields updated by the javascript unless some action is taken such as having someone click on a submit button. Even though my javascript is executed on the onload event when the web page is loaded, I still cannot access the updated fields until some other action is taken such as the viewer clicking on a submit button which I don't want to do. I then set up a timer control which I can intercept when it comes back from the server but this makes the web page reload again and is an annoyance to the viewer. The problem comes down to the fact that fields updated by javascript do not make their values available until some other event takes place after the initial load of the web page. Does anyone have a way to get around that other than using a timer control in C#?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-06 : 15:10:07
quote:
I still cannot access the updated fields until some other action is taken such as the viewer clicking on a submit button which I don't want to do
That's not true. Your JavaScript can initiate a form.submit() event, or call the page's postback() routine. Either one will submit the form back to the web server, and you can program it so that it requires no other user interaction.

ASP.Net works on top of HTTP, and sends web controls via POST. JavaScript can modify any control on the page, or even add new ones. These will all be POST'ed back to the web server during form.submit().

As I said earlier, if you don't have an explicit ASP.Net control bound for your GPS values, you can still access an HTML form element using the Request object on the server side.

The only serious problem with this method is that it runs contrary to the ASP.Net programming model. It was much easier and more natural in regular ASP, and it's Microsoft's fault for making .Net the way it is. Having said that, it's not hard to do once you figure it out, and you can probably work up a custom control or class to handle this in a more ASP.Net-like manner.
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2011-04-14 : 00:30:33
I apologize for my late reply but I have been on the road this past week. Thanks for your advice. I will try the form.submit() event in javascript and see how that works. I am mostly a C# and NET programmer and still have not become proficient in javascript. However, with the use of these forums and experts like you I hope to become more accomplished. On the subject of Microsoft, I have been trying to use the navigator.geolocation in javascript to determine the location of my viewers using their IP address. However, I am finding out that Internet Explorer 9 is the only Microsoft browser which supports this function even though FireFox and Chrome already support it. The problem is most of my viewers have not installed IE 9.
Go to Top of Page
   

- Advertisement -