Jump to content

[SOLVED] Ajax function not returning proper value.


rekha

Recommended Posts

Hi

 

I am having a customer list page.I have a delete button using anchor tag.If I want to delete a customer an alert should come and if the customer is having a pending invoice then one more alert should come.I have written this using ajax page by getting customer id.

 

function Isconfirm(id)			
{
    var  aid          = id;
    var  http_request = false;
    var s;
    if (window.XMLHttpRequest)  // For Mozilla, Safari,...
        http_request = new XMLHttpRequest();
    else if (window.ActiveXObject)  // For IE
    {
      try
      {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        try {
             http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
      }
    }
    if (!http_request)
    {
      alert('Giving up  Cannot create an XMLHTTP instance)');
      return false;
    }
    
    http_request.onreadystatechange = function() {s=alertContents(http_request);

};
    http_request.open('GET', "./getcusid.php?id="+aid, true);
    http_request.send(null);
  	 if(s === false)
{
alert("not entered");
return false;
}
else
{
return true;
}

}
function alertContents(http_request)
{
  if (http_request.readyState == 4)
  {
    if (http_request.status == 200)
    {
      var tid = http_request.responseText;
var res = confirm('Are you Sure you want to delete this customer?');
    if(res && tid!=0)
  var res1 = confirm('The selected customer has a non-zero balance.Deleting this customer will cause an adjusting transaction to be created.Is this Ok?');
     else 
    	return false;
   if (res1)
       return true;
else 
return false;
}
else
    alert('There was a problem with the request.');
}
}

 

The getcusid.php page returns the value of tid;The above ajax code is  returning true  value if ok or cancel button is clicked.

 

Please Help............

 

Link to comment
Share on other sites

Hi

 

I am having a customer list page.I have a delete button using anchor tag.If I want to delete a customer an alert should come and if the customer is having a pending invoice then one more alert should come.I have written this using ajax page by getting customer id.

 

function Isconfirm(id)			
{
    var  aid          = id;
    var  http_request = false;
    var s;
    if (window.XMLHttpRequest)  // For Mozilla, Safari,...
        http_request = new XMLHttpRequest();
    else if (window.ActiveXObject)  // For IE
    {
      try
      {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        try {
             http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
      }
    }
    if (!http_request)
    {
      alert('Giving up  Cannot create an XMLHTTP instance)');
      return false;
    }
    
    http_request.onreadystatechange = function() {s=alertContents(http_request);

};
    http_request.open('GET', "./getcusid.php?id="+aid, true);
    http_request.send(null);
  	 if(s === false)
{
alert("not entered");
return false;
}
else
{
return true;
}

}
function alertContents(http_request)
{
  if (http_request.readyState == 4)
  {
    if (http_request.status == 200)
    {
      var tid = http_request.responseText;
var res = confirm('Are you Sure you want to delete this customer?');
    if(res && tid!=0)
  var res1 = confirm('The selected customer has a non-zero balance.Deleting this customer will cause an adjusting transaction to be created.Is this Ok?');
     else 
    	return false;
   if (res1)
       return true;
else 
return false;
}
else
    alert('There was a problem with the request.');
}
}

 

The getcusid.php page returns the value of tid;The above ajax code is  returning true  value if ok or cancel button is clicked.

 

Please Help............

 

 

Hello rekha,

 

Its simple if your print variable before

if(s==false) it will print 'undefined' .

you have defined var s in the function but it is initialsed in other funtion either you make s global  in this script or edit your script as follow

  http_request.onreadystatechange = function() {s=alertContents(http_request);

if(s === false)

{

alert("not entered");

return false;

}

else

{

alert('return ture');

return true;

}

It work gr8 in both way

hope you have good time ahead in coding

 

regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.