473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How can I use ajax twice on one page to call different php pages

65
Hi All,

Im hoping somebody could help me with this. Ajax is not my strongest point.

On a page I have a requirement to call 2 different php pages using ajax. this code works fine, but if i add another is seems to conflift and im insure how.

the calls are in different areas of the page.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function showHint(str)
  5. {
  6.   if (window.XMLHttpRequest)
  7.     xmlhttp = new XMLHttpRequest();
  8.   else
  9.     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  10.  
  11.   xmlhttp.onreadystatechange = function()
  12.   {
  13.     if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
  14.       document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
  15.   };
  16.   xmlhttp.open("GET", "console4.php" + str, true);
  17.   xmlhttp.send(null);
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <script type="text/javascript">showHint(this.value)</script>
  23.  
  24. <p><span id="txtHint"></span></p> 
  25. </body>
  26. </html>
  27.  
  28.       <script type="text/javascript">
  29.       function keepCalling() {
  30.       showHint("");
  31.       window.setTimeout("keepCalling()", 2000) ;// call the showHint every (2)000 second
  32.       }
  33.       keepCalling();
  34.       </script>
  35.  
Jul 23 '10 #1
2 2666
whitep8
65
i have just tried renaming xmlhttp to xmlhttp2 but it didnt work.

i have made a few small changes in that the second call is called show, rather than showhint, just so i can ensure each one is different
Jul 23 '10 #2
dgreenhouse
250 Expert 100+
Use the same Ajax call (generalize it) for the various page sections. When the function is called it will determine which script/page section is currently being operated on (possibly using a switch statement). You may want to use the setInterval() method versus the setTimeout() method. That way, your code doesn't have to keep calling the same function over and over again.

Set some type of flag that is used by the call to determine which server script to call and the page section {element(s)} that require(s) updating.

You could also use a factory method to instantiate multiple xhr objects, but some browsers have limited number of simultaneous xhr requests active.

These are just some initial thoughts, but my belief is your code will have to be refactored to allow for what you want to accomplish.
Jul 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bruce W...1 | last post by:
I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate PHP file to support each HTML PHP page. This would be the equivalent of an ASP.NET code-behind file but using PHP....
0
by: James | last post by:
Hi, I am trying to build a shopping cart for my DVD website and am having trouble reading variable over different pages. I have a page that allows the user to add things to their cart and this...
23
by: Dufe | last post by:
Hello all: To deal with the problem of differing user screen resolutions, I've explored: 1) making the pages in PHP, 2) having different pages on the same page and selecting the proper one via...
35
by: Dirk Bruere at Neopax | last post by:
Is it possible to have one table somehow 'called' to appear on different pages? I want something to appear on all pages, yet only want to (re)edit one item. -- Dirk The Consensus:- The...
2
by: houstoncity | last post by:
Hi, I am wondering how to write the code that allows navigating to different pages based on user response. I have one question on the first page that has option (a), (b), (c), and (d) for the...
2
by: stephan.krebs | last post by:
Hi there, i get data from 4 different pages with asp 3.0 and Microsoft.XMLHTTP. It is fine and stable but it takes 15 secondes. the reason is (i think) that the requests is one after the other....
15
by: demiourgos | last post by:
Is there a way to embed html page from different server into another html page without using javascript ?
2
by: tony wong | last post by:
i find some web sites show different pages by user's click on the different headers. most important is the client browser does not reload from the remote server. may i know have some info /...
0
by: Miaaa Mukherjee | last post by:
I m using ASP.Net with VB,C# in 2005 I want to use the master page and content pages. I m creating a project which contains a leftmenu,headercontrol of images,middle menu. I don't understand...
2
by: JM_newsgroups | last post by:
Hi, When I do a postback on a particular AJAX-enabled page, I get a popup that says: "Sys.WebForms.PageRequestManagerParserErrorException: The message received could not be parsed. . . . " It...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.