claracruz
asked on
Ajax Javascript Loop Help
Hello experts,
I have three forms that with a textbox that need to be updated when a form from a first form is clicked.
The problem is that this only works properly when I am testing the values of variables with an alert prompt like so:-
function getPrice(storage_option_id , product_id)
{
var x = storage_option_id;
arrX = x.split('#')
storage_option_id = arrX[1];
var i = 1 ;
var running = true ;
while( running )
{
var product_size_id = document.getElementById( 'psID' + i ) ;
var storage_type_id = document.getElementById( 'z' + i );
var base_id = document.getElementById( 'b' + i );
if( product_size_id )
{
size_id = product_size_id.value;
storage_type_id = storage_type_id.value;
base_id = base_id.value;
//proceed as normal
var url="getprice.asp?sid=" + Math.random() + "&o=" + product_id + "&p=" + size_id + "&q=" + storage_type_id + "&r=" + base_id + "&s=" + storage_option_id + "&formCnt=" + i
alert(url) <------------------------- ---------- TESTING VALUE OF 'url'
xmlHttp=GetXmlHttpObject(s tateChange dForOpt)
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
}
else
{
running = false ;
}
i++ ;
}
}
function stateChangedForOpt()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="compl ete")
{
var t = xmlHttp.responseText;
arrtT = t.split('#')
var i = 1 ;
var price = document.getElementById( 'price' + arrtT[1]);
price.value = '£' + arrtT[2];
}
}
But when I remove the alert prompt, only the textbox in the last form gts updated.
I have three forms that with a textbox that need to be updated when a form from a first form is clicked.
The problem is that this only works properly when I am testing the values of variables with an alert prompt like so:-
function getPrice(storage_option_id
{
var x = storage_option_id;
arrX = x.split('#')
storage_option_id = arrX[1];
var i = 1 ;
var running = true ;
while( running )
{
var product_size_id = document.getElementById( 'psID' + i ) ;
var storage_type_id = document.getElementById( 'z' + i );
var base_id = document.getElementById( 'b' + i );
if( product_size_id )
{
size_id = product_size_id.value;
storage_type_id = storage_type_id.value;
base_id = base_id.value;
//proceed as normal
var url="getprice.asp?sid=" + Math.random() + "&o=" + product_id + "&p=" + size_id + "&q=" + storage_type_id + "&r=" + base_id + "&s=" + storage_option_id + "&formCnt=" + i
alert(url) <-------------------------
xmlHttp=GetXmlHttpObject(s
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
}
else
{
running = false ;
}
i++ ;
}
}
function stateChangedForOpt()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="compl
{
var t = xmlHttp.responseText;
arrtT = t.split('#')
var i = 1 ;
var price = document.getElementById( 'price' + arrtT[1]);
price.value = '£' + arrtT[2];
}
}
But when I remove the alert prompt, only the textbox in the last form gts updated.
ASKER
I have xmlHttp declared as global.
So I understand why the problem is occuring, but how do I resolve it
So I understand why the problem is occuring, but how do I resolve it
Don't declare it as global...
Remove the:
var xmlHttp ;
line you probably have at the top level of your script, and change you AJAX request code from:
xmlHttp=GetXmlHttpObject(s tateChange dForOpt)
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
to:
var xmlHttp=GetXmlHttpObject( function()
{
if( xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
var t = xmlHttp.responseText;
arrtT = t.split('#')
var i = 1 ;
var price = document.getElementById( 'price' + arrtT[1]);
price.value = '£' + arrtT[2];
}
} )
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
That *should* work...
*crosses fingers*
Tim
Remove the:
var xmlHttp ;
line you probably have at the top level of your script, and change you AJAX request code from:
xmlHttp=GetXmlHttpObject(s
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
to:
var xmlHttp=GetXmlHttpObject( function()
{
if( xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
var t = xmlHttp.responseText;
arrtT = t.split('#')
var i = 1 ;
var price = document.getElementById( 'price' + arrtT[1]);
price.value = '£' + arrtT[2];
}
} )
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
That *should* work...
*crosses fingers*
Tim
Sorry...
I meant:
var xmlHttp=GetXmlHttpObject( function()
{
if( xmlHttp.readyState == 4 && xmlHttp.status == 200 )
{
var t = xmlHttp.responseText ;
arrtT = t.split('#') ;
var i = 1 ;
var price = document.getElementById( 'price' + arrtT[1]) ;
price.value = '£' + arrtT[2] ;
}
} ) ;
xmlHttp.open("GET", url , true) ;
xmlHttp.send(null) ;
(added semi-colons)
Tim
I meant:
var xmlHttp=GetXmlHttpObject( function()
{
if( xmlHttp.readyState == 4 && xmlHttp.status == 200 )
{
var t = xmlHttp.responseText ;
arrtT = t.split('#') ;
var i = 1 ;
var price = document.getElementById( 'price' + arrtT[1]) ;
price.value = '£' + arrtT[2] ;
}
} ) ;
xmlHttp.open("GET", url , true) ;
xmlHttp.send(null) ;
(added semi-colons)
Tim
(you can then get rid of the stateChangedForOpt() function)
ASKER
Its still only updating the text box in the last form....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.drakware.com/articles/multijax.php
Shows a method of using an array to keep many simultaneous asynchronous requests going at the same time as well ;-)
Tim
Shows a method of using an array to keep many simultaneous asynchronous requests going at the same time as well ;-)
Tim
Looks like currently, call 2 is overriding call 1, call 3 is overriding #2, and that's why only the last one gets evaluated...
You haven't declared xmlHttp as global have you?