Home | Projects | Tutorials | Articles | live chat | Submit Project | Big Vote
 
Ajax Projects
.NET Frameworks
Java Frameworks
PHP Frameworks
Ruby Frameworks
Other Frameworks
Cool AJAX sites
Ajax Resources
Ajax Tools
JavaScript frameworks
Partners
Software Development

High quality software development at low prices. PHP, ASP.NET, AJAX

eBuddy

eBuddy articles and instant messenger tips

Stock Exchange Chat

Stock exchange community, chat room for each quote

Web 2.0 Sites

Web 2.0 reviews, articles, cool sites, screenshots, tips...

Self Imrovement

Videos for self improvement, self help, communication skills

Self Help

Articles for self improvement, self help, communication skills

Facebook Applications

Do you want to know the latest facebook applications?

super king bed sheet

Visit Aqttan online store for famous egyptian cotton home textile products.

 Home /  Tutorials / Ajax Effects With jQuery

Ajax Effects With jQuery





jQuery is an extremely small, yet fast and reliable JavaScript library. With jQuery you can do so much more using less code.

Read The Full Tutorial.



jQuery is an extremely small, yet fast and reliable JavaScript library. With jQuery you can do so much more using less code. "Write Less, Do More, JavaScript Library". This includes Ajax! With jQuery you can make complex HTTP requests with an extremely small amount of code. The advantages of jQuery is just far too long to list, so i'm going to show you a simple (yet effective) Ajax example which uses jQuery, and includes some effects.

View Example (Note: The background fade is a jQuery plugin named colorBlend).

Now although this examples does not contain any type of form validation. It gives you a basic idea of what you can achieve from using a JavaScript library such as jQuery and the power and additional functionality it provides.

First you must include jQuery in the head of your page, once we've done that we can begin to work with it and use the advantages it provides. In this example i'll be using jQuery.ajax( options ).

$(function(){
  $('#update').submit(function(){
    $.ajax({
      type: 'POST',
      url: 'update.php',
      dataType: 'html',
      data: { name: $('#name').val(), age: $('#age').val() },
      success: function(data){
        $('#results').prepend(data);
      },
      error: function(){
        alert("An error has occurred. Please try again.");
      },
      complete: function() {
        $('#results tr:first').fadeIn('slow');
        $('#results tr:first td').colorBlend([{ fromColor: '#FFF', toColor: '#FBB117', param: 'backgroundColor', cycles: 1 }])
      }
    });
    return false;
  });
});

Ok, now let's analyze this code. $(function(){...}); is basically short for window.onload(function(){...}); but is much more reliable. The next part is selecting the element with an ID of 'update' which in this case is the form and adding an event handler to it. So when the form is submitted it'll execute the function inside it. I make the call to submit.php' which will then query the database and return the result. In the request, i'm sending the values of any element with an ID of name and age which will be the 2 input fields on the form. If the request is successful I will then add the data returned to the beginning of the table. If an error occur's you will receive a prompt alerting you. Once the request has completed, I will then fade in the first row of the table, and then fade the background color of both columns in the first row.

Remember: You must return false; at the end of the submit() handler, otherwise the page will still refresh!

The code within this handler is the most important part, this is what makes the HTTP request.

$.ajax(
  type: // Defines the type of request, POST / GET methods.
  url: // Specifies the URL to make the request to.
  dataType: // Specifies which format the data being received is in. (HTML, XML, JSON, script etc..)
  data: // This tells jQuery what values to send when making the request. For example, i'm getting the value of the name and age input fields.
  success: // Which function is to be executed when a successful request is made.
  error: // Which function is to be executed if an error occurs.
  complete: // Which function is to be executed when the request is completed.
);

Quite simple eh? There are more parameters you could additionally add. But it wasn't necessary for me to use them for a simple example. Ok, so let's look at the PHP which handles the request.

<?php
  $name = mysql_real_escape_string(htmlspecialchars(htmlentities($_POST['name'])));
  $age = mysql_real_escape_string(htmlspecialchars(htmlentities($_POST['age'])));

  if(isset($name) && isset($age)) {
    $con = mysql_connect('localhost', 'db_user', 'db_pass');
    mysql_select_db('db_name');
    $sql = mysql_query(sprintf("INSERT INTO `db_name` . `tbl_name` ( `name`, `age` ) VALUES ( '%s', '%s')", $name, $age)) or die('Error: ' . mysql_error());
    if($sql) {
      $new = mysql_query("SELECT * FROM `db_name` . `tbl_name` ORDER BY `id` DESC LIMIT 1") or die('Error: ' . mysql_error());
      while($row = mysql_fetch_array($new)) {
        print '<tr style="display: none;" class="updated">';
        print '<td>' . $row['name'] . '</td>' . '<td>' . $row['age'] . '</td>';
        print '</tr>';
      }
    }
  }
?>

The first part is what secures the data ready for inserting into the database. $_POST['name'] and $_POST['age'] get's the POST data which has been sent by Ajax and stores them in variables for use later. The if statement is then used to determine whether the variables have been set or not and depending if it returns TRUE or FALSE it'll execute the code. If it returns TRUE the database connection is made and the database is selected. Once that's finished, it'll insert the data into the database. If the query is successful it'll proceed to retrieve the newly inserted data. But we only want to return one value. So we use MySQL statements to do so. Once we retrieve it from the database, it'll be put into the correct format and then printed. However, the display of the element is set to none, this is because jQuery will handle it. $('#results tr:first').fadeIn('slow'); will change the display and show it, but add some cool effects while doing so.

In conclusion, JavaScript libraries are more logical to use, and much more reliable. Most libraries are extremely dynamic enabling you to alter almost any and all functionality of it. I use jQuery, because it's designed to be small, fast and to enable a sophisticated level of functionality.

source: codera

Related Tutorials

  • 9 Tips to Smaller & Optimized CSS Files
    Because CSS files are loaded from inside the tags, every visitor download these files. We optimize images and PHP files, but CSS files are often overlooked. We should though and that’s what we will do today.
  • Very simple jQuery AJAX PHP chat
    jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages. jQuery is designed to change the way that you write JavaScript.
  • Html Form submit using Ajax
    Its not easy to submit HTML forms through Ajax. Forms input parameters are not send automatically during form submission. We manually collect all input parameters and send it separately as query string.
  • Slideshare Gallery Viewer, no API needed
    For developers, having an API available to access information from your favorite site is a good thing. It's not always necessary, though, as shown by Christian Heilmann in his latest post. Christian walks us through the process of creating a Slideshare ga
  • JavaScript Programming Patterns
    JavaScript is meant to be used to add behaviour to a website, might it be for form validation or for more complex operations like drag & drop functionality or performing asynchronous requests to the webserver (aka Ajax). During the past few years, JavaScr
  • Ajax and XML
    With the advent of widely available broadband, media, movies, images, and sound drive the Web 2.0 revolution. Learn to combine media with technologies such as PHP and Asynchronous JavaScript™ + XML (Ajax) to create a compelling experience for your custome
Says:
Fri Aug 08, 2008 4:06 am
Says:
Fri Aug 08, 2008 4:06 am
Says:
Tue Nov 11, 2008 10:53 am
Software Development Company Says:
Mon Nov 24, 2008 7:07 am
Excellent.....

Leave Your Comment

Name (Required)
Mail (will not be published) (required)
Website
Top Projects
e-messenger
MSN Web Messenger
ebuddy
MessengerFX
ILoveIM
AJAX file upload
You Tube
KoolIM.com
Ajax.NET Professional
Meebo
Tutorials
RESTTest HTTP Tester
3D-style animation in JavaScript
Ajaxed: Ajax for classic ASP
A Safe Communication Mechanism
Making DIV a Link - Javascript Vs CSS
AJAX - JSON vs. XML
jQuery AJAX PHP chat upgrade
Ajax Generic Comments Module
Bug with Ajax HTML Grid and File Upload Forms
ASP.NET 2.0 Script CallBack ( AJAX like)