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

retreive data using javascript and php

Status
Not open for further replies.

jtaal

Technical User
Jan 17, 2004
23
NL
Does anyone know a way to retreive - platform/browser independently! - data using javascript and php.

The situation is like this: i'm building a page with several basic elements, like textboxes checkboxes etc.
Another - less basic - element, called listview, has a great deal of relevant information, this information is stored in a database and is retreived by php. This retreiving can take some time, because sometimes several JOINs have to be made and that requires a lot of searching.
Anyway, I can set up a php page to generate javascript code, or even the listview html code.
My goal is to load all this data after the whole page is loaded, somewhat like
Code:
<body onLoad=&quot;loadForeignData()&quot;>
.

I tried IFRAME but thats IE only and ILAYER is NS only so these are not gonna work...

Please help
 
You could use a regular frameset to do this cross broswer (and deprecating into version 4 broswers nicely too). You can set the frame to be 0 pixels... and it will not show on any IE browsers; other browsers (NS) will display a 1 or 2 pixel frame (but easily hidden if you set the background color on both frame contents to be the same etc).

Your onload would then set the document.location for the hidden frame to be a php file that returns your data and builds up the javascript you need on the php file. Assuming this is a function, you can just access it from the frame directly.. no need to pass the data back into the main frame.

Just an early morning rant... I hope it has some relevance to your project.

Jeff
 
Jtaal...

Any news on this? I was hoping you would have posted back some feedback on the problem. The problem is fairly common - and I think the general concensus is to use some kind of frame (iframe or not).

Just keen to see what direction you went in.

Jeff
 
This is what i ended up with:
Code:
<script language=&quot;JavaScript&quot;>
<!-- Data retrieval functions via xmlhttp request
var xmlhttp = null;
var xmlhttpSupport = false;
var xmlhttpDestination = null;
var xmlhttpCustomCallback = null;
var xmlhttpParser = null;

function loadXmlhttpSupport()
{
  if (xmlhttp !== null) return;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
    try { xmlhttp = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;); } 
    catch (e) {
      try { xmlhttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;); }  
      catch (E) { xmlhttp = false; }
    }
  @else
    xmlhttp = false;
  @end @*/
  if (!xmlhttp) {
   try { xmlhttp = new XMLHttpRequest(); } 
   catch (e) { xmlhttp = false; }
  }
  if (!xmlhttp) xmlhttpSupport = false;
  else xmlhttpSupport = true;
}
loadXmlhttpSupport();

function onReadyStateWriteToDestination() 
{
  if (xmlhttp.readyState == 4) // if finished
  {
    if (xmlhttpDestination != null) 
    {
      if (xmlhttpParser != null) eval(xmlhttpDestination + ' = ' + xmlhttpParser + '(xmlhttp.responseText)');
      else eval(xmlhttpDestination + ' = xmlhttp.responseText');
    }
    if (xmlhttpCustomCallback != null) eval(xmlhttpCustomCallback);
  }
}

function dataParser(text)
{
  dataArray=text.split(&quot;#*DataSeparator*#&quot;);
  /* Do something with other data */
  return dataArray[0];
}

function retrieveData(objectID, query) 
{  
  if (xmlhttp) { 
    xmlhttpDestination = &quot;document.getElementById('&quot; + objectID&quot;').innerHTML&quot;;
/*    xmlhttpCustomCallback = 'callbackfunction(&quot;' + &quot;callbackargs&quot; + '&quot;)'; */
    xmlhttpParser = 'dataParser';
    xmlhttp.open(&quot;GET&quot;, &quot;data.php?query=&quot; + escape(query), true);
    xmlhttp.onreadystatechange=onReadyStateWriteToDestination;
    xmlhttp.send(null);
  }
}

function retrieveAllData()
{
  retrieveData('destination1', 'SELECT * FROM users WHERE user_id = 1');
  retrieveData('destination2', 'SELECT * FROM users WHERE user_id = 2');
 /* etc... */
}

//-->
</script>
<body onLoad=&quot;retrieveAllData()&quot;>

The script uses an Javascript object to retreive data using http. It retreives data from a php script data.php (duh!) using a SQL query.
The general idea is that you call retrieveData with 2 parameters, the first one must be a string with the id of (preferably) a DIV tag ie: <div id='destination1' ...>
the other one is the query. One can also rewrite retrieveData to accept a http:// adres or some other relative path.

It is known to work in IE versions from IE 5.5 (maybe 5.0??) Netscape >6.0 Mozilla 1.4 (dunno about earlier versions)
Unfortunately it doesn't work in opera, it doesn't have the XMLHttpRequest object (obviously it doesn't have the activeX built in).
in IE4 and early versions of netscape there is an error caused by try and catch. I don't know which version of javacript is needed and i don't know which version IE 4 hasnot and IE 5 has...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top