473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ajax Example

iam_clint
1,208 Recognized Expert Top Contributor
AJAX Basic Example
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. //set the variables.
  3. function getArticle(id) {
  4.     var link = "/getNews.asp";
  5.     xmlhttpPost(link, id);
  6. }
  7.  
  8. //Make the actual connection.
  9. function xmlhttpPost(strURL, id) {
  10.     var xmlHttpReq = false;
  11.     var self = this;
  12.     // Mozilla/Safari
  13.     if (window.XMLHttpRequest) {
  14.         self.xmlHttpReq = new XMLHttpRequest();
  15.     }
  16.     // IE
  17.     else if (window.ActiveXObject) {
  18.         self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  19.     }
  20.     self.xmlHttpReq.open('POST', strURL, true);
  21.     self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  22.     self.xmlHttpReq.onreadystatechange = function() {
  23.         if (self.xmlHttpReq.readyState == 4) { //ready state 4 means its complete.
  24.             updatepage(self.xmlHttpReq.responseText);
  25.         }
  26.     }
  27.     self.xmlHttpReq.send(getquerystring(id));
  28. }
  29.  
  30. //set the query string to be sent
  31. function getquerystring(id) {
  32.     qstr = 'data=' + escape(id);  // NOTE: no '?' before querystring
  33.     return qstr;
  34. }
  35.  
  36. //put the data on the page.
  37. function updatepage(str) {
  38.     document.getElementById("data").innerHTML = str;
  39. }
  40. </script>
  41. <select onchange="getArticle(this.value);">
  42. <option value="1">Train hits car</option>
  43. <option value="2">Airplane Crashes</option>
  44. <option value="3">Mental Health</option>
  45. </select>
  46. <div id="data"></div>
  47.  

Now for the getNews.asp --- not fully functional code but an example
Expand|Select|Wrap|Line Numbers
  1. <%
  2. news_id = request("id")
  3. strSQL = "select * from news where news_id = " & new_id
  4. rs.open strSQL, connection, 1, 1
  5. if not rs.eof then
  6. response.write rs("story")
  7. end if
  8. %>
  9.  
Jun 21 '07 #1
20 33089
herocks
5 New Member
Mind saying what the code dose?
Jun 24 '07 #2
iam_clint
1,208 Recognized Expert Top Contributor
it does an ajax query.
Jun 25 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
Mind saying what the code dose?
It gets the news articles from a database by selecting from a drop down without having to refresh/reload the page.

If you don't know Ajax is, maybe you should check out some tutorials.
Jun 25 '07 #4
herocks
5 New Member
Sorry for my noobness. I looked at some AJAX tutorials and this makes a lot more sense but a few things I still don't get...
Jun 29 '07 #5
acoder
16,027 Recognized Expert Moderator MVP
Sorry for my noobness. I looked at some AJAX tutorials and this makes a lot more sense but a few things I still don't get...
Like ?
Jun 29 '07 #6
herocks
5 New Member
Eh, I just needa study the code more...
Jul 1 '07 #7
cheogt
5 New Member
Expand|Select|Wrap|Line Numbers
  1. ajax=function(strURL,sSend){
  2.     var xmlHttpReq=false
  3.     var self=this
  4.     if (window.XMLHttpRequest)self.xmlHttpReq=new XMLHttpRequest()
  5.     else if(window.ActiveXObject)self.xmlHttpReq=new ActiveXObject("Microsoft.XMLHTTP")
  6.     self.xmlHttpReq.open('POST',strURL,false)
  7.     self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  8.     self.xmlHttpReq.onreadystatechange=function(){
  9.         if (self.xmlHttpReq.readyState==4){
  10.             ajax.resul=self.xmlHttpReq
  11.         }
  12.     }
  13.     self.xmlHttpReq.send(sSend)
  14. }
  15.  
Hello i have made a little change on the main function, so you can use it like this:
Expand|Select|Wrap|Line Numbers
  1. function getArticle(id){
  2.   ajax('dataExample.php',id) // php,asp,...
  3.   alert(ajax.resul.responseText)
  4. }
  5.  
Not a big deal but now you can use the data loaded directly with the javascript, and no need to load the data into a div continer, i hope it can be helpfull
Oct 31 '07 #8
cheogt
5 New Member
Sorry o forgot to tell that i couldn't make it work with Firefox, it works only with ie, if some one find a way.. please :)
Oct 31 '07 #9
acoder
16,027 Recognized Expert Moderator MVP
Sorry o forgot to tell that i couldn't make it work with Firefox, it works only with ie, if some one find a way.. please :)
ajax.resul.resp onseText is only available after the readyState is 4, so the call must be made within the anonymous function onreadystatecha nge or a function within that anon. function.
Oct 31 '07 #10

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

Similar topics

5
1490
by: cbmeeks | last post by:
I am trying to learn AJAX to do some cool stuff over the internet. So, for practise, I decided to code a small site that simply prints out quotes from William Shakespeare. http://eblarg.com/quotes/quotes.aspx Basically, start typing away and watch the quotes appear. See how many you can guess! If this becomes popular, I will add tons more quotes.
0
1406
by: aaronwmail-usenet | last post by:
I've published sample code that uses Python on the server side to implement AJAX type ahead completion for web forms. Please see documentation with links to examples and downloads at http://xsdb.sourceforge.net/xFeed.html "Type ahead completion" is a form of AJAX (asyncronous javascript with XML) functionality where the page attempts to suggest completions
0
1611
by: aaronwmail-usenet | last post by:
xFeed AJAX data publication example code with documentation is available: See http://xsdb.sourceforge.net/xFeed.html for documentation. See http://www.xFeedMe.com for live web page examples. See http://sourceforge.net/project/showfiles.php?group_id=93603 to download. The xFeed example application of the
2
2132
by: dileep | last post by:
I am new to Ajax and i found some good tutorials from the net.but when i downloaded the basic sample examples and try to execute i get the error "specified file not found", even though the files are placed in the same folder itself.i checked the whole code again and again but could not do anything.Can any one please help me out with this. Is it necessary to have a separate server to execute these examples as i have just downloaded the...
25
2783
by: meltedown | last post by:
This is supposed ot be an example: http://www.ajaxtutorial.net/index.php/2006/11/30/simple-ajax-using-prototype-part-2/ It says : This example is probably the simplest example you will ever find. We are going to use the prototype feature ‘ajax.Updater’ (see part one for more details on prototype).
2
1532
RamananKalirajan
by: RamananKalirajan | last post by:
Hi Guys I am new to AJAX. I am in need of an AJAX example which includes Prototype. My Requirement is from a prototype i must create a AJAX call and call a HTML file or Java file without server and print the response back in the caller file. If you guys can help me put pls help me Regards Ramanan Kalirajan
0
8253
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7182
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6116
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.