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!

AJAX works when synchronized but not when asynchronized.

Status
Not open for further replies.

wu78754

Programmer
Nov 6, 2009
12
US
The following code doesn't replace the ? with data when running using call back function. But works when not using call back function. I would prefer not to use synchronize mode to avoid looking stalled when waiting. Can you help?
URL:
<?php
function usIndex($str)
{
$row = explode(",",trim($str));
echo '<TABLE border="1">';
echo '<TR>';
echo '<TH>Symbol</TH><TH>High</TH><TH>Current</TH><TH>Potential</TH>';
echo '</TR>';
$ratio = 0;
$dr = 0;
foreach ($row as $rv) {
// $rv = historicalShort($rv);
$rv = '?,?,?,?';
echo '<TR>';
$col = explode(',', $rv);
if ($col[3] > $ratio) {
$symbol = $col[0];
$highest = $col[1];
$current = $col[2];
$ratio = $col[3];
}
$cc = 0;
foreach ($col as $cv) {
echo "<TD><DIV id='x$dr$cc'>$cv</DIV></TD>";
$cc++;
}
echo '</TR>'."\n";
$dr++;
}
echo '</TABLE>';
}
?>
<HTML>
<HEAD>
<script type="text/javascript">
function loadXMLDoc(url,r)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url);
XMLHttp.onreadystatechange = function() {
if (XMLHttp.readyState == 4) {
var data = xmlhttp.responseText.split(",");
for (var c=0;c<4;c++) {
var id = "x" + r + c;
document.getElementById(id).innerHTML=data[c];
}
}
}
xmlhttp.send(null);
}
</script>
</HEAD>
<BODY>
<?php
if (isset($_GET['index'])) {
$index = $_GET['index'].'.txt';
$str = file_get_contents($index);
$str = str_replace("\n", ",", $str);
usIndex($str);
}
?>
<a href="?index=dji" target="dji">Dow Jones Industrial</a>
<a href="?index=sp100" target="sp100">S & P 100</a>
<a href="?index=sp500" target="sp500">S & P 500</a>
<form action="?" target="stock">
<input type="text" name="stock" value="C">
<input type="submit" value="Submit">
</form>
<script type="text/javascript">
var stocks = "<?php echo $str;?>";
var sym = stocks.split(",");
for (i=0; i<sym.length; i++) {
url = "getstock.php?stock=" + sym;
loadXMLDoc(url,i);
}
</script>
</BODY>
</HTML>
 
Hi

Your code should never work.
[ul]
[li]JavaScript is case sensitive.[/li]
[li]You should use local variables for [tt]XMLHttpRequest[/tt].[/li]
[/ul]
Then will work.
Code:
[b]function[/b] [COLOR=darkgoldenrod]loadXMLDoc[/color][teal]([/teal]url[teal],[/teal]r[teal])[/teal]
[teal]{[/teal]
    [highlight][b]var[/b] xmlhttp[teal];[/teal][/highlight]
    [b]if[/b] [teal]([/teal]window[teal].[/teal]XMLHttpRequest[teal])[/teal]
    [teal]{[/teal][gray]// code for IE7+, Firefox, Chrome, Opera, Safari[/gray]
        xmlhttp[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]XMLHttpRequest[/color][teal]();[/teal]
    [teal]}[/teal]
    [b]else[/b]
    [teal]{[/teal][gray]// code for IE6, IE5[/gray]
        xmlhttp[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]ActiveXObject[/color][teal]([/teal][green][i]"Microsoft.XMLHTTP"[/i][/green][teal]);[/teal]
    [teal]}[/teal]
    xmlhttp[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal][green][i]"GET"[/i][/green][teal],[/teal]url[teal]);[/teal]
    [highlight]xmlh[/highlight]ttp[teal].[/teal]onreadystatechange [teal]=[/teal] [b]function[/b][teal]()[/teal] [teal]{[/teal]
        [b]if[/b] [teal]([/teal][highlight]xmlh[/highlight]ttp[teal].[/teal]readyState [teal]==[/teal] [purple]4[/purple][teal])[/teal] [teal]{[/teal]
            [b]var[/b] data [teal]=[/teal] xmlhttp[teal].[/teal]responseText[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]","[/i][/green][teal]);[/teal]
            [b]for[/b] [teal]([/teal][b]var[/b] c[teal]=[/teal][purple]0[/purple][teal];[/teal]c[teal]<[/teal][purple]4[/purple][teal];[/teal]c[teal]++)[/teal] [teal]{[/teal]
                [b]var[/b] id [teal]=[/teal] [green][i]"x"[/i][/green] [teal]+[/teal] r [teal]+[/teal] c[teal];[/teal]
                document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal]id[teal]).[/teal]innerHTML[teal]=[/teal]data[teal][[/teal]c[teal]];[/teal]
            [teal]}[/teal]
        [teal]}[/teal]
    [teal]}[/teal]
    xmlhttp[teal].[/teal][COLOR=darkgoldenrod]send[/color][teal]([/teal][b]null[/b][teal]);[/teal]
[teal]}[/teal]

Feherke.
 
The synchronize version works. See
function loadXMLDoc(url,r)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url,false);
xmlhttp.send(null);

var data = xmlhttp.responseText.split(",");
for (var c=0;c<4;c++) {
var id = "x" + r + c;
document.getElementById(id).innerHTML=data[c];
}
}
 
Hi

wu78754 said:
The synchronize version works.
Yepp. Because it not contains the case mismatch I mentioned earlier.

By the way, each response is about 40~45 byte. Probably would be faster ( possibly much faster ) to request and serve multiple information in once. To handle them I would use JSON.

Feherke.
 
Thanks, feherke.

Would you provide a JSON example for this one?
 
Hi

Currently your response looks like this :
Code:
MMM,89.64,83.50,1.07
and is processed by this :
Code:
[b]var[/b] data [teal]=[/teal] xmlhttp[teal].[/teal]responseText[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]","[/i][/green][teal]);[/teal]
[b]for[/b] [teal]([/teal][b]var[/b] c[teal]=[/teal][purple]0[/purple][teal];[/teal]c[teal]<[/teal][purple]4[/purple][teal];[/teal]c[teal]++)[/teal] [teal]{[/teal]
  [b]var[/b] id [teal]=[/teal] [green][i]"x"[/i][/green] [teal]+[/teal] r [teal]+[/teal] c[teal];[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal]id[teal]).[/teal]innerHTML[teal]=[/teal]data[teal][[/teal]c[teal]];[/teal]
[teal]}[/teal]
Using JSON, your PHP script should return this ( easiest to generate it with [tt]json_encode()[/tt] ) :
Code:
["MMM",89.64,83.5,1.07]
and processed it with this :
Code:
[b]var[/b] data [teal]=[/teal] [highlight][COLOR=darkgoldenrod]eval[/color][teal]([/teal][/highlight]xmlhttp[teal].[/teal]responseText[teal][highlight])[/highlight];[/teal]
[b]for[/b] [teal]([/teal][b]var[/b] c[teal]=[/teal][purple]0[/purple][teal];[/teal]c[teal]<[/teal][purple]4[/purple][teal];[/teal]c[teal]++)[/teal] [teal]{[/teal]
  [b]var[/b] id [teal]=[/teal] [green][i]"x"[/i][/green] [teal]+[/teal] r [teal]+[/teal] c[teal];[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal]id[teal]).[/teal]innerHTML[teal]=[/teal]data[teal][[/teal]c[teal]];[/teal]
[teal]}[/teal]
Pretty similar. But becomes easier to transform it into this :
Code:
[["MMM",89.64,83.5,1.07],["ABT",57.02,55.35,1.03],["ANF",80.86,32.97,2.45]]
and processed it with this :
Code:
[b]var[/b] data [teal]=[/teal] [COLOR=darkgoldenrod]eval[/color][teal]([/teal]xmlhttp[teal].[/teal]responseText[teal]);[/teal]
[highlight][b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]data[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal][/highlight]
  [b]for[/b] [teal]([/teal][b]var[/b] c[teal]=[/teal][purple]0[/purple][teal];[/teal]c[teal]<[/teal][purple]4[/purple][teal];[/teal]c[teal]++)[/teal] [teal]{[/teal]
    [b]var[/b] id [teal]=[/teal] [green][i]"x"[/i][/green] [teal]+[/teal] [highlight]revsym[teal][[/teal]data[teal][[/teal]i[teal]][[/teal][purple]0[/purple][teal]]][/teal][/highlight] [teal]+[/teal] c[teal];[/teal]
    document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal]id[teal]).[/teal]innerHTML[teal]=[/teal]data[highlight][teal][[/teal]i[teal]][/teal][/highlight][teal][[/teal]c[teal]];[/teal]
  [teal]}[/teal]
[highlight][teal]}[/teal][/highlight]
Where revsym is the reverse of sym ( [tt]sym[2]='ANF'[/tt] -> [tt]revsym['ANF']=2[/tt] ), created like this :
Code:
[b]var[/b] stocks [teal]=[/teal] [green][i]"MMM,ABT,ANF,...,ZION"[/i][/green][teal];[/teal]
[b]var[/b] sym [teal]=[/teal] stocks[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]","[/i][/green][teal]);[/teal]
[highlight][b]var[/b] revsym[teal]=[/teal][teal]{}[/teal][/highlight]
[highlight][b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]sym[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] revsym[teal][[/teal]sym[teal][[/teal]i[teal]]]=[/teal]i[/highlight]
Of course, you can do the above quite easily with two [tt]split()[/tt]s. JSON's utility becomes evident when using more complicated data structures with associative arrays.

[small][maroon]Warning[/maroon] The above code was not tested[/small]

Feherke.
 
Hi

By the way, I just hate the way you are using those [tt]id[/tt]s. Here is a rewrite in my style :
HTML:
[b]<html>[/b]

[b]<head>[/b]

[b]<script[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text/javascript"[/i][/green][b]>[/b]
function loadXMLDoc(url)
{
  var xmlhttp;
  if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
  }
  xmlhttp.open('GET',url);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      var data=eval(xmlhttp.responseText);
      for (var i=0,l=data.length;i<l;i++) {
        var r=revsym[data[i][0]]+1;
        for (var c=0;c<4;c++) stocktab.rows[r].cells[c].innerHTML=data[i][c];
      }
    }
  }
  xmlhttp.send(null);
}
[b]</script>[/b]

[b]</head>[/b]

[b]<body>[/b]

[b]<table[/b] [maroon]border[/maroon][teal]=[/teal][green][i]"1"[/i][/green] [maroon]id[/maroon][teal]=[/teal][green][i]"stocktable"[/i][/green][b]>[/b]
[b]<tr><th>[/b]Symbol[b]</th><th>[/b]High[b]</th><th>[/b]Current[b]</th><th>[/b]Potential[b]</th></tr>[/b]
[b]<tr><td>[/b]?[b]</td><td>[/b]?[b]<td>[/b]?[b]</td><td>[/b]?[b]</td></tr>[/b]
[b]<tr><td>[/b]?[b]</td><td>[/b]?[b]<td>[/b]?[b]</td><td>[/b]?[b]</td></tr>[/b]
[b]<tr><td>[/b]?[b]</td><td>[/b]?[b]<td>[/b]?[b]</td><td>[/b]?[b]</td></tr>[/b]
[b]<tr><td>[/b]?[b]</td><td>[/b]?[b]<td>[/b]?[b]</td><td>[/b]?[b]</td></tr>[/b]
[b]<tr><td>[/b]?[b]</td><td>[/b]?[b]<td>[/b]?[b]</td><td>[/b]?[b]</td></tr>[/b]
[b]</table>[/b]

[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"?index=dji"[/i][/green] [maroon]target[/maroon][teal]=[/teal][green][i]"dji"[/i][/green][b]>[/b]Dow Jones Industrial[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"?index=sp100"[/i][/green] [maroon]target[/maroon][teal]=[/teal][green][i]"sp100"[/i][/green][b]>[/b]S & P 100[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"?index=sp500"[/i][/green] [maroon]target[/maroon][teal]=[/teal][green][i]"sp500"[/i][/green][b]>[/b]S & P 500[b]</a>[/b]

[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]"?"[/i][/green] [maroon]target[/maroon][teal]=[/teal][green][i]"stock"[/i][/green] [maroon]method[/maroon][teal]=[/teal][green][i]"post"[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"stock"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"C"[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Submit"[/i][/green][b]>[/b]
[b]</form>[/b]

[b]<script[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text/javascript"[/i][/green][b]>[/b]
var stocktab=document.getElementById('stocktable')

var stocks='MMM,ABT,ANF,ADBE,AMD'
var sym=stocks.split(',');
var revsym={};
for (var i=0,l=sym.length;i<l;i++) revsym[sym[i]]=i;

// send a separate request or each stock
var url;
for (var i=0,l=sym.length;i<l;i++) {
  url='getstock.php?stock='+sym[i];
  loadXMLDoc(url);
}

// send a single request for all stocks
var url='';
for (var i=0,l=sym.length;i<l;i++) url+=(url?'&':'getstock.php?')+'stock[]='+sym[i];
loadXMLDoc(url);

[b]</script>[/b]

[b]</body>[/b]

[b]</html>[/b]
By the way, test data was generated by this PHP script :
Code:
[teal]<?php[/teal]

[navy]$resp[/navy][teal]=[/teal][b]array[/b][teal]();[/teal]

[b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]is_array[/color][teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'stock'[/i][/green][teal]]))[/teal] [teal]{[/teal] [gray]// multiple stocks[/gray]
  [b]foreach[/b] [teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'stock'[/i][/green][teal]][/teal] [b]as[/b] [navy]$one[/navy][teal])[/teal]
    [COLOR=darkgoldenrod]array_push[/color][teal]([/teal][navy]$resp[/navy][teal],[/teal][b]array[/b][teal]([/teal][navy]$one[/navy][teal],[/teal][COLOR=darkgoldenrod]rand[/color][teal](),[/teal][COLOR=darkgoldenrod]rand[/color][teal](),[/teal][COLOR=darkgoldenrod]rand[/color][teal]()));[/teal]
[teal]}[/teal] [b]else[/b] [teal]{[/teal] [gray]// single stock[/gray]
  [COLOR=darkgoldenrod]array_push[/color][teal]([/teal][navy]$resp[/navy][teal],[/teal][b]array[/b][teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'stock'[/i][/green][teal]],[/teal][COLOR=darkgoldenrod]rand[/color][teal](),[/teal][COLOR=darkgoldenrod]rand[/color][teal](),[/teal][COLOR=darkgoldenrod]rand[/color][teal]()));[/teal]
[teal]}[/teal]

[COLOR=darkgoldenrod]header[/color][teal]([/teal][green][i]'Content-type: text/javascript'[/i][/green][teal]);[/teal]
[b]echo[/b] [COLOR=darkgoldenrod]json_encode[/color][teal]([/teal][navy]$resp[/navy][teal]);[/teal]

[teal]?>[/teal]

Feherke.
 
Feherke,

Thank you for the contribution. I suppose the code is tested and I can just use it.

Below is the getstock($sym) code. I think make a call to get all stocks at once will create too much delay that looks stalled.

$d = date('j'); //day of month 1-31
$m = date('n'); //month 1-12
$y = date('Y'); //2010
$m--; //month 0-11
if ($m < 10) $m = '0'.$m;
$url=" $str = @file_get_contents($url);
$row = explode("\n",trim($str));
$rc = 0;
$highest = 0;
$current = 0;
foreach ($row as $rv) {
$rc++;
$col = explode(',', $rv);
if ($col[6] > $highest) $highest = $col[6]; //6 is adjusted close price
if ($rc == 2) $current = $col[6]; //last close price
}
if (strlen($str)) $ratio = number_format($highest/$current, 2, '.', '');
else $ratio = 0;
$str = "$sym, $highest, $current, $ratio";
echo $str;
 
Hi

wu78754 said:
I think make a call to get all stocks at once will create too much delay that looks stalled.
Ah. I supposed you are querying a local database.

However, requesting all daily quotes since January 2, 1962 until today each time a visitor comes to that page, is abit brutal. I would seriously think in using a local database for caching. ( Assuming that Yahoo!'s copyright allows it. )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top