Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Posting data to database 1

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
0
0
GB
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="<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
 
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 [ignore]
Code:
[/ignore] tags.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
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...')
  }
}
 
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 [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
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>
 
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?
 
My apologies - I cleared my browser cache on IE8 and it started working. Thanks for the tip re: document.write - very helpful!

Ed
 
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:

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top