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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript And Data From Database

Status
Not open for further replies.

likelylad

IS-IT--Management
Joined
Jul 4, 2002
Messages
388
Location
GB
Hi

I was wondering if someone would help me with this problem as my Javascript skills are very limited.

I am trying to get data to be displayed from a database on a form when a particular cell in a table is clicked.

I was using the following link as my reference guide


The following is the main form.
Code:
<Html>
<head>
<script src="selectwarehouse.js"></script>
</head>
<body>


<form name="users">
<table>
<tr>
<td>
<table border=2>
<tr bgcolor='#dfdfff'><td><b><center>Warehouse<center></b></td><td><b>Passed</b></td><td><b><center>Failed</center></b></td><td><b><center>Percent</center></b></td></tr>
<tr><td value="DE" onclick="showData(this.value)"><Center>IR</Center></a></Center></td><td><Center>60447</Center></td><td><Center>30470</Center></td><td><Center>66&#37;</Center></td></tr>
</table>
</td>
<td>
<div id="txtHint"><b>Click On Warehouse To See Info Here</b></div>
</td>
</tr>
</table>
</form>

</body>
</html>

The Code For the selectwarehouse.js file is as below
Code:
var xmlHttpfunction showData(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
var url="getinfo.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

I am guessing something in the selectwarehouse.js needs to be changed but I don't know what!

Thanking in advance for any help received
 
So... what was the problem you were experiencing then?

The markup you pasted is all-kinds-of-bad (including unbalanced tags). I would suggest that you should get your markup validating to a doctype (start with 4.0 transitional) first. Then you should start work on any particular problem. Only then can you be sure that the problem is with the Javascript... and more importantly... start to fix it.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Your JS code is missing semicolon line delimiters - which is ultimately the root cause of your problem.

If you had a delimiter after the first 11 bytes:

Code:
var xmlHttp[!];[/!]

then the following function definition would have been distinct.

I'd say you should follow Jeff's advice, and then go and nicely format your JS - it would make debugging these issues far easier in the future.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I went back to have a look at

And I found the following code that worked for me

Code:
var xmlHttp

function showData(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML=""
  return
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
var url="gethint.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

Thanks for your previous comments
 
Me said:
If you had a delimiter after the first 11 bytes then the following function definition would have been distinct.

You said:
I went back to have a look at And I found the following code that worked for me

Yes - but spot the difference. Their code has delimiters (albeit new lines instead of semicolons). Yours did not.

You should use semicolons anyway (and I find it hard to believe a site that purports to be a learning tool does not), as some browsers (Safari being one) will throw errors in some circumstances if they are not present.

Take that advice or leave it, but leave it at your own risk.

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top