Avatar of roblickley
roblickley
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Help with AJAX javascript please

I have attached the code to the javascript file I am using to perfom an AJAX task.

It is the basis for a transaction page, which allows the user to approve or remove certain contents in an order, before payment is taken.

I have the asp page set up to call javascript function approve(ID) and remove(id) using buttons - where the id relates to the ItemID in the recordset.

In order to actively update the page I have set each group of buttons within a span, which I have named Process + ItemID - thus unique for each line i.e Process29384

I am trying to get the script to process updates for each line and then selectively change the span from buttons to a text message when the processing is done.

I think I am missing something  - probably simple, as I want to pass the initial variable (str) to the stateChanged function so it only updates the specific span referenced.

any help appreciated

PS it is not showing up properly on my preview, however I have correctly referenced the ElementById("Process"+str) the + appears to be missing!
function approve(str)
{
if (str.length==0)
  { 
  document.getElementById("Process"+str).innerHTML="";
 
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="approve.asp";
url=url+"?EntryID="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged(str);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
 
function remove(str)
{
if (str.length==0)
  { 
  document.getElementById("Process"+str).innerHTML="";
 
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="remove.asp";
url=url+"?EntryID="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged(str);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
 
 
 
 
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;
}
 
 
function state_Changed(str)
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('Process'+str).innerHTML=xmlhttp.responseText
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}

Open in new window

JavaScriptAJAX

Avatar of undefined
Last Comment
roblickley

8/22/2022 - Mon
roblickley

ASKER
I should have said that the error retuned is that :
stateChanged is not defined
sunithnair

You have named your function state_Changed and not stateChanged
sunithnair

Try this one
function approve(str)
{
if (str.length==0)
  { 
  document.getElementById("Process"+str).innerHTML="";
 
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="approve.asp";
url=url+"?EntryID="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=state_Changed(str);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
 
function remove(str)
{
if (str.length==0)
  { 
  document.getElementById("Process"+str).innerHTML="";
 
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="remove.asp";
url=url+"?EntryID="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=state_Changed(str);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
 
 
 
 
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;
}
 
 
function state_Changed(str)
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('Process'+str).innerHTML=xmlhttp.responseText
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
Morcalavin

You would need to do:
xmlHttp.onreadystatechange=function(){state_Changed(str)};

ASKER CERTIFIED SOLUTION
sunithnair

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Morcalavin

You can pass parameters to onreadystatchanged by wrapping it in an anonymous function as demonstrated in my post above:
xmlhttp.onreadystatechanged = function(){ some_sub_with(args)};
roblickley

ASKER
Thanks - I knew it would be something silly!

Now, however, the span on the master page is not updating.

In IE7 it is reporting an error - in Firefox it is not. In neither is it actually working.

IE7 error is Not Implemented

running Firebug I can see the response from the action file (I.e the remove or approve) is returning as expected.

I had some incorrect capitilisation which I have altered.

I have inserted an alert command in the stateChanged function and nothing happening. Am I missing something?
// JavaScript Document
// Used to manage approval of paid entries
 
function approve(str)
{
if (str.length==0)
  { 
  document.getElementById("Process"+str).innerHTML="";
 
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="approve.asp";
url=url+"?EntryID="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged(str);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
 
function remove(str)
{
if (str.length==0)
  { 
  document.getElementById("Process"+str).innerHTML="";
 
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="remove.asp";
url=url+"?EntryID="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged(str);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
 
 
 
 
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;
}
 
 
function stateChanged(str)
{
if (xmlHttp.readyState==4)
  {// 4 = "loaded"
  if (xmlHttp.status==200)
    {// 200 = "OK"
	alert(xmlHttp.responseText);
    document.getElementById("Process"+str).innerHTML=xmlHttp.responseText;
	
    }
  else
    {
    alert("Problem retrieving data:" + xmlHttp.statusText);
    }
  }
}

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
roblickley

ASKER
btw I posted this before seeing the above - apologies
roblickley

ASKER
result thanks!
roblickley

ASKER
Thanks, you anticipated my next question even before I posted it.
I am new to Javascript and AJAx and your help has been invaluable
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23