CHAMPION KAY TUTORIAL

Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

ajax - creating an html form

Before we can start getting to the exciting new stuff, we must first make a standard HTML form (no submit button though!). This form will be spiced up in later with a hint of Ajax, but for now let's just make a solid, basic HTML form with a couple inputs.


Ajax? do you have the time:





To keep this Ajax easy to understand, we are going to be creating an HTML form that has two text fields: name and time. The name field will be filled in by the user, while the time field will be filled in using Ajax.
Below is the HTML code for your "order.html" webpage. If you would like to refresh your knowledge of forms, then check out our HTML forms lesson.

order.html HTML Code:

<html>
<body>

<form name='myForm'>
Name: <input type='text' name='username' /> <br />
Time: <input type='text' name='time' />
</form>

</body>
</html>
Save this file as "order.html"

Where?  the  submit  button?

That's the great thing about Ajax, you do not need a form submit button to send the user's data to the server. We are going to be using our "Javascript on Steroids" to get and submit data with the server.
Now that we have our HTML form, we can dive deeper into the Ajax jungle and try to discover what we're facing.

This lesson includes one of the largest hurdles for aspiring Ajax programmers:browser support. It would be nice if all the web browsers required the same Javascript code to use Ajax, but life isn't fair and you've got your work cut out for you!

This lesson will show you how to create the keystone of Ajax; the XMLHttpRequest object. Not only will you know how to make this important Ajax object, but you will also know how to make it compatible with all the popular browsers: Internet Explorer, Opera, Firefox, and Safari.

Ajax try/catch blocks of code?

To create this important Ajax object, you are going to have to use a special programming technique known as "try and catch". Basically it attempts to "try" a piece of code and if that piece causes an error it "catches" the error and keeps going. Normally when an error occurs the code will stop running, however, the "catch" eats up the error and lets the code continue.
In the following code we are going to "try" three different ways to make a new XMLHttpRequest object. Every time we fail and get an error, we will catch the error and try the next a different command.
Note: If our "try" is successful then the "catch" code will not be run because it is only used when there is an error.

order.html Javacsript Code:

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!
 
 try{
  // Opera 8.0+, Firefox, Safari
  ajaxRequest = new XMLHttpRequest();
 } catch (e){
  // Internet Explorer Browsers
  try{
   ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try{
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e){
    // Something went wrong
    alert("Your browser broke!");
    return false;
   }
  }
 }
}
//-->
</script>



<form name='myForm'>
Name: <input type='text' name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>
In the above Javascript code, we try three times to make our XMLHttpRequest object. Our first attempt:
  • ajaxRequest = new XMLHttpRequest();
is for the Opera 8.0+, Firefox and Safari browsers. If that fails we try two more times to make the correct object for an Internet Explorer browser with:
  • ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  • ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");>
If that doesn't work, then they are using a very outdated browser that doesn't support XMLHttpRequest, which also means it doesn't support Ajax.
Most likely though, our variable ajaxRequest will now be set to whateverXMLHttpRequest standard the browser uses and we can start sending data to the server.




Although Adode Flash has improved performance-wise (arguably) recently and more people are upgrading to more powerful systems (desktops, laptops, notebooks, netbooks, tablets and other mobile web hardware) – still flash cannot offer what the world has been looking for. That is – even better performance cross-platform, more openness and better security (to name a just a few). Obviously the blanket ban flash has been given from some Apple products (think Iphone/Ipad) is not helping.
For the past couple of years, web technology has exploded in terms of what can be achieved using JavaScript. Things have also moved forward with HTML and CSS. The terms used to describe these recent improvements are HTML5.
Apple has mentioned many times that flash is not needed and that “everything that can be done in Flash, can be done using HTML5” (that is – HTML, JavaScript, CSS and perhaps Canvas). They are not the first to say this (Apple usually pick-up on what other already know!).
With the Iphone and Ipad selling like hotcakes, web application providers previously using Flash (or considering using it) need to have a serious think about the user reach. They could do the old-fashion thing – build a Flash web application and an alternative HTML 4 one. Or they could build one in HTML 5. Now that’s a hard one!
There are various JavaScript libraries out there with some great UI. But one of the newest ones using the very latest technologies comes from the team that brought us EXT JS (now run by Sencha). It’s called Sencha Animator. Is Sencha Animator a serious flash alternative?
Sencha Animator comes as a desktop application used to create rich HTML5 animation. It’s currently in its infancy but could have real potential. There is one drawback at this point-in-time however; it requires a Webkit browser to work. This means it will work in Google Chrome, Apple Safari, Iphone, Ipad, Blackberry torch, Google Android and other Webkit based browsers. But it will not fully work in IE, Opera or Firefox. At least not until the CSS3 is fully supported (IE 9 claims support).
At this time, many of the CSS3 techniques used by Sencha Animator are Webkit only (i.e. it has things like: -webkit-text-fill-color and -webkit-transform).
Sooner or later all major browsers should support these… but just not yet.

Basic Ajax usage with jQuery


in this article we will be talking about the basic usage of Ajax with jQuery 1.4.2 production (24KB, Minified and Gzipped).
In order to use the JavaScript examples in this article, you should first download jQuery and include it on your page using:
  1. <script type="text/javascript" src="jquery-1.4.2.js"></script>  
Throughout this article our Ajax scripts will communicate with ‘serverscript.php‘ our Server script.
  1. <?php  
  2. if(isset($_POST['firstname']) &&  
  3.    isset($_POST['lastname'])) {  
  4.  echo "Hey {$_POST['firstname']} {$_POST['lastname']}, 
  5.    you rock!\n(repsonse to your POST request)";  
  6. }  
  7. if(isset($_GET['firstname']) &&  
  8.    isset($_GET['lastname'])) {  
  9.  echo "Hey {$_GET['firstname']} {$_GET['lastname']}, 
  10.    you rock!\(response to your GET request)";  
  11. }  
  12. ?>  
Method One – POST (Asynchronous with data):
Transfer data to the server (using POST method), and retrieve a response:
  1. function doAjaxPost() {  
  2.  $.ajax({  
  3.    type: "POST",  
  4.    url: "serverscript.php",  
  5.    data: "firstname=clint&lastname=eastwood",  
  6.    success: function(resp){  
  7.      // we have the response  
  8.      alert("Server said:\n '" + resp + "'");  
  9.    },  
  10.    error: function(e){  
  11.      alert('Error: ' + e);  
  12.    }  
  13.  });  
  14. }  
  15.   
  16. doAjaxPost();  
Method Two – GET (Asynchronous with data):
Transfer data to the server (using GET method), and retreive a response:
  1. function doAjaxGet() {  
  2.  $.ajax({  
  3.    type: "GET",  
  4.    url: "serverscript.php",  
  5.    data: "firstname=clint&lastname=eastwood",  
  6.    success: function(resp){  
  7.      // we have the response  
  8.      alert("Server said:\n '" + resp + "'");  
  9.    },  
  10.    error: function(e){  
  11.      alert('Error: ' + e);  
  12.    }  
  13.  });  
  14. }  
  15.   
  16. doAjaxGet();  
Note that GET is the default type for Ajax calls using jQuery, so we really do not need to explicitly state it, but I’ve placed it there just for clarity.
Practical jQuery Example using POST:
  1. <html>  
  2. <head>  
  3.   
  4. <script type="text/javascript" src="jquery-1.4.2.js"></script>  
  5. </head>  
  6. <body>  
  7.   <script>  
  8. function doAjaxPost() {  
  9.  // get the form values  
  10.  var field_a = $('#field_a').val();  
  11.  var field_b = $('#field_b').val();  
  12.   
  13.  $.ajax({  
  14.    type: "POST",  
  15.    url: "serverscript.php",  
  16.    data: "firstname="+field_a+"&lastname="+field_b,  
  17.    success: function(resp){  
  18.      // we have the response  
  19.      alert("Server said:\n '" + resp + "'");  
  20.    },  
  21.    error: function(e){  
  22.      alert('Error: ' + e);  
  23.    }  
  24.  });  
  25. }  
  26. </script>  
  27.   
  28. First Name:  
  29. <input type="text" id="field_a" value="Sergio">  
  30. Last Name:  
  31. <input type="text" id="field_b" value="Leone">  
  32. <input type="button" value="Ajax Request" onClick="doAjaxPost()">  
  33.   
  34. </body>  
  35. </html>