Advertisement

01.20.2008 at 08:29PM PST, ID: 23097460
[x]
Attachment Details

Multiple AJAX Functions on One Page - Each Connecting to a Different PHP File and Sending Response to a Different Span Tag

Asked by FrankTech in JavaScript

Tags: AJAX, JavaScript, IE6

I have two AJAX Javascripts. Each works perfectly when it is the *only* AJAX script on the page.  But script 1 does not work properly when script 2 is also in the page.  I need both.
   
   When both scripts are on the page, the response from the 1st script is wrongly showing up in the <span> where the 2nd script's response should go.

                      * It should work like this: *

    1) Click link 1 to fire AJAX function 1, which connects to PHP script 1 and sends response to <span id="1"></span>
   2) Change option in <select id="2"> to fire AJAX function 2, which connects to PHP script _2_ and sends response to <span id="2">.
                   
                   But, instead, it is wrongly doing this:
   AJAX function 1 fires, connects to PHP file 1, AND WRONGLY SENDS ITS RESPONSE TO <span id="2"> INSTEAD OF <span id="1">. (The second function works fine, however.)

      The mis-placed response problem does not happen if I remove or disable the 2nd AJAX function.  (When the 2nd AJAX function is commented out, the 1st function sends its response properly to <span id="1">, as it should.)

The code snippet below shows both functions.  Something in function 2 is interfering with function 1.  How can I make them both work?  Please show exactly what I need to change in one or both of the JS scripts.
     There will be two more JS/AJAX scripts on the page, also.  In total, there will be four AJAX scripts on this one page, each connecting to a different PHP script, and each sending the response to a different <span> tag. So the solution needs to work with multiple AJAX scripts on the same page.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
// FUNCTION 1 ******************* //
 
<script language="JavaScript" type="text/javascript">
 var xmlHttp
function ajaxFunction1() {
 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
 
 var url="phpfile1.php";
 url=url+"?sid="+Math.random();
 xmlHttp.onreadystatechange=stateChanged;
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
}
 
function stateChanged() { 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
    document.getElementById("1").innerHTML=xmlHttp.responseText;
  } 
} 
 
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;
}
</script>
 
 
// ************* SCRIPT 2, with FUNCTION 2 *********** //
 
<script language="JavaScript" type="text/javascript">
 
 var xmlHttp
function ajaxFunction2() {
 
xmlHttp= GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
 
 var url="phpscript2.php";
  url=url+"?sid="+Math.random();
 xmlHttp.onreadystatechange=stateChanged;
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
}
 
function stateChanged() { 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
    document.getElementById("2").innerHTML=xmlHttp.responseText;
  } 
} 
 
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;
}
</script>
 
Keywords: Multiple AJAX Functions on One Page …
 
Loading Advertisement...
 
[+][-]01.20.2008 at 08:43PM PST, ID: 20703951

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2008 at 09:19PM PST, ID: 20704037

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2008 at 10:09PM PST, ID: 20704164

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.20.2008 at 11:00PM PST, ID: 20704303

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.20.2008 at 11:22PM PST, ID: 20704364

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 05:17AM PST, ID: 20705724

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 06:46AM PST, ID: 20706362

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 06:59AM PST, ID: 20706439

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: AJAX, JavaScript, IE6
Sign Up Now!
Solution Provided By: FrankTech
Participating Experts: 2
Solution Grade: B
 
 
[+][-]01.23.2008 at 02:30AM PST, ID: 20722212

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.25.2008 at 10:58AM PST, ID: 20745247

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628