Nov 21, 2009

Mootools 1.2 Ajax Request - How to request data from another web and insert into div

free web hosting
Open Discussion > MODERATED AREA > Tutorials

Mootools 1.2 Ajax Request - How to request data from another web and insert into div

Pankyy
Just in the last tutorial I made a few days ago, the central topic of this tutorial is Javascript/AJAX and how to use to request data from a web when asked, and insert its data into a HTML div container; and also, how to run an action when request has been finished. This is also a script I have tested, cause I'm using it for my website so you can rest assure that it will work. You'll need the Mootools necessary version here.

Like the last one, the first line you have to put into your script is:

CODE
<script type="text/javascript" src="js/mootools.js"></script>


where "js/mootools.js" is the directory where we have put the mootools js code in our webhost.

Next code is going to be a large one:

CODE
<script language="javascript">
window.addEvent('domready', function() {

     $('runAjax').addEvent('click', function(event) {
         event.stop();
         var req = new Request({
             method: 'get',
             url: $('runAjax').get('href'),
             data: { 'do' : '1' },
             onRequest: function() { alert('The request has been made, please wait until it has finished.'); },
             onComplete: function(response) { alert('The response is the following : ' + response); }
         }).send();
$('runAjax').removeEvent('click');
     });
});
</script>


The first part (
CODE
window.addEvent('domready', function() {})
) is to initiate the DOM, which is an API for HTML documents, that we are going to use to comunicate with the other page, and do correct use of Mootools.

CODE
$('runAjax').addEvent('click', function(event) { event.stop();
'runAjax' is the id that the link is going to have (I'll post complete code at final to compare). What this does is stop the clicking event, which would take you to the actual web, to make it backend, and request it. The url, then, the href=''; one, the one the link was going to
CODE
url: $('runAjax').get('href'),)
where runAjax is the same link id.

The most important lines to the people that want to use this codes are the following:
CODE
onRequest: function() { alert('The request has been made, please wait until it has finished.'); },
onComplete: function(response) { alert('The response is the following : ' + response); }


onRequest is when the request of that file starts, you can put any javascript code in there to make the user wait.
onComplete is when the request was completed. It's made in function of the response, cause we are receiving it, and we can use it to report something to the user.

Attention: if you want to refresh a div with new data (for example, the response), then this part of the code would have to be like this:

CODE
onRequest: function() { alert('The request has been made, please wait until it has finished.'); },
  onComplete: function(response) { alert('Response received.); $('divID').set('html', response); }


That will output the response in the div cointainer with ID 'divID' and will also alert that the response has been received.


So, the final code to use this would be:

CODE
<HTML>
<head>
<script type="text/javascript" src="js/mootools.js"></script>
<script language="javascript">
  window.addEvent('domready', function() {
  
      $('runAjax').addEvent('click', function(event) {
          event.stop();
          var req = new Request({
              method: 'get',
              url: $('runAjax').get('href'),
              data: { 'do' : '1' },
              onRequest: function() { alert('The request has been made, please wait until it has finished.'); },
              onComplete: function(response) { alert('The response is the following : ' + response); }
          }).send();
$('runAjax').removeEvent('click');
      });
  });
  </script>
</head>
<body>
<a href='webtovisit.php' id='runAjax'>Click here to make the ajax request</a>
<div id='divID'></div> ///// THIS IF YOU ARE GOING TO REFRESH THE DIV INFO.
</body>
</HTML>


Hope this helps one to understand this, I had some hard time understanding this the first time; everyone seemed to pick it up quickly and I didn't.

 

 

 


Comment/Reply (w/o sign-up)

Ash-Bash
Wow thanks for the tutorial I just downloaded some Ajax scripts today and was wondering how to install and use them, Thank you for this right at the time I need them I will be installing them now Muhahahaha Then take over the world with "Ajax Script" Lol tongue.gif

Comment/Reply (w/o sign-up)

sonesay
Whats wrong with your link? http://mootools.net/ is the correct URL why are you providing an indirect link that doesn't seem to load?

http://mootools.net/download/get/mootools-1.2.1-core-yc.js

Comment/Reply (w/o sign-up)

Pankyy
QUOTE (sonesay @ Feb 11 2009, 03:24 AM) *
Whats wrong with your link? http://mootools.net/ is the correct URL why are you providing an indirect link that doesn't seem to load?

http://mootools.net/download/get/mootools-1.2.1-core-yc.js


It seems I had a problem when I edited the post and the link contained the redirect page of the forums once again and it didn't work.

Comment/Reply (w/o sign-up)

(G)Adam Meyer
Wrong code
Mootools 1.2 Ajax Request

Your code actually has some issue in it.

Your are attaching the event listener to a link with the ID of "runAjax"

Every time you click on this, you are creating a new instance of the request. Not a good thing as it uses more resources than it should

You should create the instance of the request, then in the eventListener just have req.Send(); return false;

This will make just one instance of the request class no matter how many time you click it.

The code you have you have would work if you are attaching it to a class, because a new instance may be needed due to having many urls

but even then, there are better ways to do this.

-reply by Adam Meyer

Comment/Reply (w/o sign-up)

Pankyy
QUOTE ((G)Adam Meyer @ Feb 20 2009, 07:23 PM) *
Wrong code
Mootools 1.2 Ajax Request

Your code actually has some issue in it.

Your are attaching the event listener to a link with the ID of "runAjax"

Every time you click on this, you are creating a new instance of the request. Not a good thing as it uses more resources than it should

You should create the instance of the request, then in the eventListener just have req.Send(); return false;

This will make just one instance of the request class no matter how many time you click it.

The code you have you have would work if you are attaching it to a class, because a new instance may be needed due to having many urls

but even then, there are better ways to do this.

-reply by Adam Meyer


It may be kind of late but I just read this. Yes, there are more efficient ways of doing it, but this was intended to be made as a built-in script, to show it's use and everyone could make it work the way they think is the best for the browsers not to overload.
Again, about creating new instances, that's true too, but I was just showing the example with only one. I'm not a Javascript expert myself, in fact I'm pretty bad, but I have tested this and got good results, so I thought it would be cool sharing.

Thanks, though, I have added the remove.Events().

 

 

 


Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords :

  1. Create Dynamic Html/php Pages Using Simple Vb.net Code
    Taking your application data, and creating a webpage for others to vie (1)
  2. Getting Started With Mysql
    creating tables and insert data into them. (2)
    Hi in this tutorial you will learn how to create tables and insert items into them. First steps are
    to create the database - go into your cpanel and mysql databases, from there make an account and a
    database and then attach them together with all priviliges, call the database test and the account
    admin, with the pw as pass - or any other password. We need to connect to the database so first in
    your php file (probably named index.php) - this is how to do it. CODE
    mysql_connect("localhost", "admin", "pass") or die(mysql_error()); mysql_select_db("test") or
    die(mysql_....
  3. Faux Ajax Loading - Css Only
    Pretend your site is Ajax based (3)
    Link: http://www.jlhaslip.trap17.com/samples/misc/ajax/index.html Check that out. The first page
    has information and the second and has the actual example of its use with sample CSS code. I find
    that when you visit a site which has a slow server and attempt to view 'large' Image files,
    it is pretty boring to sit and stare at a blank screen, so this little snippet of code can be used
    to give the visitor something to see to indicate that the image is being downloaded. I built a
    small animated gif that sits in the background of the space allocated for the image....
  4. Mootools - My Favourite Javascript Library
    (3)
    It kind of amazes me that there's not even a mention of the Mootools javascript library
    throughout this whole forum. So here I'll do a brief introduction and a tutorial on how to
    produce the famous accordion effect. MooTools is a compact, modular, Object-Oriented javascript
    framework designed to make writing extensible and compatible code easier and faster. MooTools lets
    you get the job done efficiently and effectively. It is slightly based on the powerful Prototype
    javascript framework , of which Scriptaculous runs on. (But frankly, I dislike Scriptaculou....
  5. How To Group Multiple Sets Of Data In Microsoft Excel 2007
    (0)
    How to Group Multiple Sets of Data in Microsoft Excel 2007 Sometimes you may open several
    workbooks and work with a number of the same workbooks at a time. You can open this group of files
    with Microsoft Excel 2007 simultaneously. But you have to define them as part of a workspace, and
    save them in a single Excel 2007 file. To do this, follow below steps: 1- Click On the View tab and
    then in the Window group click Save Workspace. The Save Workspace dialog box appears. 2- In the File
    name field, type your work name and then Click Save. 3- At the top-left of window, C....
  6. Php Form Data And Conditional Statements
    My third tutorial (3)
    Intro To PHP Tutorial 3 - Form Data and Conditional Statements Released 4/12/07 By Chris Feilbach
    aka GhostRider Contact Info: E-mail: assembler7@gmail.com AIM: emptybinder78 Yahoo:
    drunkonmarshmellows Website: http://www.ghostrider.trap17.com PART I - FORM DATA One of the
    things that makes PHP so popular is its ability to handle form data. You can do whatever you want
    with it, you can email it to yourself, store it in a database, display it on a page, you can do
    literally ANYTHING with it. There are two ways to send data to your server, GET and POST. Both
    are ....
  7. Adding Data To A Database And Displaying It Later
    Using Forms, PHP and MySQL (1)
    Requirements: PHP Support MySQL Database access I am going to use a news program as an example.
    Ok, first you are going to need to connect to the database. Do so by using the code below. I have
    added some comments where you will need to edit to fit your server's specifications. Create a
    new file with notepad and call it config.php QUOTE //Change root to your database
    account's username $dbusername = "root"; //Add your account's password in between the
    quotations $password = " "; //Add the name of the database you are using in between the qu....

    1. Looking for Mootools, 1.2, Ajax, Request

Searching Video's for Mootools, 1.2, Ajax, Request
See Also,
advertisement


Mootools 1.2 Ajax Request - How to request data from another web and insert into div

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com