开发者

simultaneously calling 2 functions via ajax with "onclick"

开发者 https://www.devze.com 2023-02-26 15:27 出处:网络
I have a problem using AJAX. I\'m new to this so the answer could be simple! So, i have this piece of code.

I have a problem using AJAX. I'm new to this so the answer could be simple!

So, i have this piece of code.

echo '<input type="button" on开发者_JAVA百科click="opinion(1,\''.$v.'\'); op_status(1,\''.$v.'\');"/>';

which is written in php.

The 2 functions that are called via the onclick event, toggle AJAX in 2 different html divs. What i get is the outcome of the second function on both divs.

Any ideas why this could be happening?

Thanks!!

sorry for the inconvenience but this is my first post. so this is one of the 2 (identical, just with different variables) js scripts.

function op_status(op,pid)
{
if (op=="")
  {
  document.getElementById("opstatus"+pid).innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("opstatus"+pid).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","post_op_stat_disp.php?ajxpid="+pid,true);
xmlhttp.send();
}

the file that is called makes some checks and outputs a text depending on that checks at a certain html div. the other js script does exactly the same, making some other tests and outputting some other text on another html div. :) however the outputs are the same text echoed in both the divs


First I'd add "var xmlhttp = null;" at the top of your function - that will locally scope the variable instead of making it global in nature - as @David Dorward points out. That would end up assigning the response handler of the ajax request in the second declared function (which ever that happens to be second) as the response handler for both ajax requests and you'd get the behavior you describe.

If you're still getting the same text in both div's then I'd suspect that both javascript functions are calling post_op_stat_disp.php instead of the opinion function calling what I would guess to be post_opinion_disp.php and as a result it is returning the same data to each div. Or (even more unlikely) that both post_opinion_disp and post_op_stat_disp are returning the same result.

Since this problem isn't really germain to AJAX but instead is likely a coding problem - I would suggest navigating manually to http://[server]/post_op_stat_disp.php?ajaxid=test and your other url and see what should be populated in both divs.

Then I would highly suggest that you abstract your ajax code to some common function so that you are not duplicating the http status code and xmlhttp instantiation logic. And though it may not help you solve this particular problem, it would reduce your code size to something along these lines if you followed @Jared Farrish's advice and used jquery:



  function op_status(op, pid) {
     if (!op) return $('#opstatus' + pid).html("");

     $.get('post_op_status_disp.php?ajaxid=' + pid, function(r) {
          $('#opstatus' + pid).html(r);
     });
  }

My money is on the var key word which will ensure you get two different xmlhttp objects instead of one global one.

But as most have already commented - you might want to post the generated results to help us out in helping you track this bugger down.

Thanks, let us know how it turns out!

0

精彩评论

暂无评论...
验证码 换一张
取 消