×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Posting data to database

Posting data to database

Posting data to database

(OP)
Hi,

I am trying to record the country and city of a visitor to my website using Ajax and ASP.

The first page the user visits has the following code:

<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAy9zYz4qMTN6QL3gFu4YqqBRomh8EuldaJ7HdeWi4uTNP0yiNeBQFon2lnczxB70cke79CwP_MlqwwA"></script>
<script src="saveloc.js"></script>
<script language="javascript">
saveLoc(google.loader.ClientLocation.address.country, google.loader.ClientLocation.address.city)
</script>

saveloc.js contains my function called saveLoc.  I've added a couple of lines to display the country and city to test the function is being called properly.  So far so good!

var xmlhttp

function saveLoc(Country, City)
{
document.write(Country)
document.write(City)
if (Country.length==0)
  {
  return;
  }
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
var url="savedata.asp";
url=url+"?Country="+Country;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

The bit that does not seem to be working is this:

var url="savedata.asp";
url=url+"?Country="+Country;
xmlhttp.open("GET",url,true);

My file savedata.asp currently contains the following:

<%
Response.Write("Saving data...")
%>

However this message isn't displayed and even if I include a deliberate mistake like

Responsse.Write("Test")

no error is returned - where am I going wrong please?  The idea is I can then add to savedata.asp so it actually writes to my database.

Thanks very much

Ed

RE: Posting data to database


How do you know nothing is being returned if you haven't defined any callback function by setting "onreadystatechange" ?

Also, if you're posting code, the formatting (assuming you have any) is kept if you surround it in [code][/code] tags.

Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Posting data to database

(OP)
I've done a bit more researchon the function you mentioned and have now changed my code to the following.  However I'm still not sure if my savedata.asp script is being executed.  Am I bit closer than I was?  Thanks.

CODE

var url="savedata.asp";
url=url+"?Country="+Country;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  document.write('Testing...')
  }
}

RE: Posting data to database


Personally, I'd install Firefox and the Firebug extension. That way, you can see all AJAX requests and their response (if any).

It's very, very handy for debugging this sort of problem.

Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Posting data to database

Hi

Just a minor note : do not use document.write() after the document was loaded. Use DOM manipulation instead.

Feherke.
http://free.rootshell.be/~feherke/

RE: Posting data to database

(OP)
OK when I run it in firefox it shows the text from the function

CODE

function stateChanged()
{
if (xmlhttp.readyState==4)
 {
 document.write('Testing...')
 }
}

However it leaves an hour glass open and the top right hand corner is indicating that its requesting data.  Only when I click the stop button does firebug display the HTML source

CODE

<html>
<head/>
<body>Testing...</body>
</html>

RE: Posting data to database

(OP)
OK - almost there!  I removed the document.write's and that fixed the hourglass issue on firefox.  It's now working in firefox but if you visit the site with IE8 it's not making the AJAX call properly - I've written a script which shows me the contents of the database.

The code that does this is

CODE

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

The strange thing is on another site of mine that uses Ajax the same code works - admittedly it does something a bit different but principle should be the same?

RE: Posting data to database

(OP)
My apologies - I cleared my browser cache on IE8 and it started working.  Thanks for the tip re: document.write - very helpful!

Ed

RE: Posting data to database

Ed,

I had a similar problem and Dan's site has an example of how to keep the browser cache from impacting your Ajax applications. I suggest you visit Dan's Page @ Code Couch: http://www.codecouch.com/dan/
 

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close